I've got a Bootstrap button group that has 2 buttons (JSON and XML) with JSON being active when the page is loaded. Pressing the XML button will automatically change focus to it.
Now, I'd like to attach some behavior to these buttons so that clicking the XML button hides the error-json pre element and shows the error-xml pre element.
Is there a Bootstrap-native way to accomplish this?
<div class="btn-group">
<button class="btn btn-default" autofocus="true">JSON</button>
<button class="btn btn-default">XML</button>
</div>
<pre id="error-json" class="hidden"><code class="language-json">{
"ErrorCode": STATUS_CODE,
"Description": ERROR_MSG
}</code></pre>
<pre id="error-xml"><code class="language-xml"><error>
<ErrorCode>STATUS_CODE</ErrorCode>
<Description>ERROR_MSG</Description>
</error>
</code></pre>
$("#error-xml").hide();
$(".btn").click(function () {
var id = $(this).attr("id");
if(id == "json"){
$(this).attr("autofocus",true).siblings().attr("autofocus",false);
$("#error-xml").hide();
$("#error-json").show();
}else{
$(this).attr("autofocus",true).siblings().attr("autofocus",false);
$("#error-json").hide();
$("#error-xml").show();
}
});
:focus{
outline: 1px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="btn-group">
<button class="btn btn-default" id="json" autofocus="autofocus">JSON</button>
<button class="btn btn-default" id="xml">XML</button>
</div>
<pre id="error-json" class="hidden"><code class="language-json">{
"ErrorCode": STATUS_CODE,
"Description": ERROR_MSG
}</code></pre>
<pre id="error-xml"><code class="language-xml"><error>
<ErrorCode>STATUS_CODE</ErrorCode>
<Description>ERROR_MSG</Description>
</error>
</code></pre>
I think this can be accomplished with css dependencies. You can use the css selector #error-json + #error-xml {
/*something here*/
}
Bootstrap has pre-builtin classes to show/hide elements. You find the classes in the documentation:
http://getbootstrap.com/css/#helper-classes-show-hide
Maybe this thread helps out:
On a CSS hover event, can I change another div's styling?
Why do you dont want to use javascript/jQuery to add the desired behaviour?
Regards
Related
I have the following code:
<div id="question" onclick="location.href='{% url 'read_question' question.id %}';" style="cursor:pointer;">
<button class="btn btn-primary" disabled>
<p>my text</p>
</button>
</div>
The result:
When I click on the div #question I go to an other page.
But when I click on the button I also go to the other page.
However my button is disabled...
I would like not to go to another page when I click on the button.
In Firefox when I click on the button nothing happens (This what I want).
But in Chrome I go to the other page...
Someone could help me to permanently disable the button?
I use HTML5 and Bootstrap 4
you have the button inside the div so when you click on the button you are also clicking on the div. You can also do it your way and just check to see if the event.taget is a button. if it is don't go to the url, if it isn't then go.
<div id="question" onclick="location.href='{% url 'read_question' question.id %}';" style="cursor:pointer;">xxx
</div>
<div>
<button class="btn btn-primary" disabled>
<p>my text</p>
</button>
</div>
Try moving the onclick to the <button> itself rather than the surrounding <div>. The disabled attribute of the button will not be able to disable the onclick applied to its parent element.
In the two examples below I have styled it so you can see the difference between the surrounding div (In blue) and the button. Notice that the alert will only fire in the top example when clicking the div.
I am assuming that your styling means you can not see the difference between the div and button and it is left for Chrome and Firefox to decide whether you are clicking the disabled button or the div.
div {
background: blue;
padding: 1em
}
<div onclick="alert('test')">
<button disabled>my text</button>
</div>
div {
background: blue;
padding: 1em
}
<div>
<button disabled onclick="alert('test')">my text</button>
</div>
function myFunction(){
console.log(event.target.innerHTML);
if(event.target.innerHTML=='div')window.location.href='{% url ' + 'read_question' + 'question.id %}';
}
<div id="question" onclick='myFunction()' style="cursor:pointer;">
<button class="btn btn-primary" >div</button>
<button class="btn btn-primary" disabled>
<p>my text</p>
</button>
</div>
If you want to keep the button in the div, you should use event.stopPropagation();. Try the following code.
<body>
<div id="question" onclick="goLink();" style="cursor:pointer;">
<button class="btn btn-primary" onclick="noLink(event);">
<p>my text</p>
</button>
</div>
<script>
function goLink(){
window.location.assign("{% url 'read_question' question.id %}");
}
function noLink(){
window.alert("I didn't go anywhere.");
event.stopPropagation();
}
</script>
</body>
There is also a event.stopImmediatePropagation(); method. I hope this helps you out.
The button is disabled and that works exactly as it should (prevents the default action of the button).
However, by default, click events are bubbling. Which effectively means any such event on any elements in your page does not only get triggered on that particular element, but on every one of its parents until document or until one of the elements in the chain stop the bubbling.
To stop a click event (or any other bubbling event) from bubbling you have to call stopPropagation() method on it.
In your case:
document.querySelector('#question btn').addEventListener('click', function(e) {
e.stopPropagation();
})
...or, in jQuery:
$('#question btn').on('click', e => { e.stopPropagation() });
Now your button will not pass the click event to the div. If it's enabled it will do what you want it to, if not, it won't. The <div> won't have a clue in either case.
If you only want to cancel the bubbling when the button is disabled, change the selector from #question btn to #question btn[disabled]
I am doing an online course on frontend, I have just started getting to know bootstrap 4 and flexbox. As far as I understand, to do inline styling is something that is considered bad practice. What I mean is this:
<button style="color: white; border: 5px solid red;"> Press me! </button>
And I like that the good practice is to not do this, mainly because of readability. What I don't understand is why the button above is not a good practice but the code here is considered good practice
<button class="btn btn-primary btn-lg d-flex justify-content-center> Press me! </button>
Just to clarify I do understand that the style that I used in the example doesn't do the same thing as the one using bootstrap. I am just interested in why one is OK and the other one is not.
The only thing that I have come up with is that since bootstrap is using class="" it's probably not inline styling.
The first instance is inline styling:
<button style="color: white; border: 5px solid red;"> Press me! </button>
and the second has several classes that are styled in a separate css file:
<button class="btn btn-primary btn-lg d-flex justify-content-center> Press me! </button>
One of the main reasons that it is bad practice to use inline styles is because they can override the styles that exist in the separate CSS file and become hard to track once your CSS becomes more complex. Also, your code becomes more difficult to maintain when you use inline styles. For example, if you had several buttons in your HTML that were each individually styled with inline styles and you decided to change one of the styles you would then have to change the style for each individual button, whereas if you gave them all the same class and styled that class in a separate CSS file, then you can change the color once and it will update all of your buttons.
For example (bad practice):
HTML
<button style="background-color: dodgerblue;">Click here</button>
<button style="background-color: dodgerblue;">Click here</button>
<button style="background-color: dodgerblue;">Click here</button>
<button style="background-color: dodgerblue;">Click here</button>
vs (good practice):
HTML
<button id="btn-one" class="button">Click here</button>
<button id="btn-two" class="button">Click here</button>
<button id="btn-three" class="button">Click here</button>
<button id="btn-four" class="button">Click here</button>
CSS
.button {
background-color: dodgerblue;
}
You can read more about CSS styling here.
I'm using the primeng and ng-bootstrap components to make a website with angular.
To capture information I use a form within an ng-bootstrap modal. When clicking on the "save" button of that modal The primeng confirmDialog should appear. The problem is that it appears behind the modal.
How can I fix it?
Here's some of my HTML:
<p-confirmDialog header="Confirmation" icon="pi pi-exclamation-triangle" width="425" appendTo="body"></p-confirmDialog>
<ng-template #modalFormOrder let-c="close" let-d="dismiss" id="modalFormOrder">
<div class="modal-header">
...
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" (click)="c('Close click')">Close</button>
<button type="button" class="btn m-btn--pill m-btn--air btn-success" (click)="SaveEditLoadOrder()">Save</button>
</div>
</ng-template>
I'm somehow sure that both dialogs use z-index CSS property
Override the z-index CSS property with a bigger number for the element that you want to appears first, example z-index: 1001;
If you want to override Css for PrimeNG I suggest you write it on /src/styles.css . Following Example is overriding calendar CSS
.ui-calendar {
width: 100%;
height: 100%;
}
On PrimeNG documentation you can find the CSS classes to override for each component
I want to select only <buttons> whose parents have display: block and exclude those <buttons> whose parents have display:none.
Is there any way to accomplish this?
Actually there's a CSS3 solution to select elements that doesn't have a display:none style, or given an explicit style property:
*:not([style*="display: none"]) button{ ... }
Demo:
*:not([style*="display: none"]) button{
color:yellow;
}
<p style="display:block">
My name is A.
<button>
a
</button>
</p>
<p style="display: none">
<button>
b
</button>
</p>
If those display styles are declared inline then you can use the following selectors: div[style*="display: none;"] (if element has inline style attribute containing "display: none;" then apply style)
Attribute Selectors:
The CSS attribute selector matches elements based on the presence
or value of a given attribute.
Src: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
Attribute Contains Selector:
When looking to find an element based on part of an attribute value,
but not an exact match, the asterisk character, *, may be used within
the square brackets of a selector. The asterisk should fall just after
the attribute name, directly before the equals sign. Doing so denotes
that the value to follow only needs to appear, or be contained, within
the attribute value.
Src: https://learn.shayhowe.com/advanced-html-css/complex-selectors/
No.
There are no selectors which select elements based on the values of properties that apply to them.
I don't think it would be practical for CSS to introduce such a feature either. Imagine:
:has-property-value(display: none) {
display: block;
}
This is not possible with pure CSS so far,
Unless you explicitly specify the inline css to style="display: none".
You could use some javascript to filter a set of buttons that are visible.
var buttons = document.querySelectorAll('.block button');
var visibleButtons = [];
buttons.forEach(function (element) {
if (window.getComputedStyle(element.parentNode).display !== 'none') {
visibleButtons.push(element);
}
});
console.log(visibleButtons);
.block {
display: block;
}
.hidden {
display: none;
}
<div class="block">
<button>btn 1</button>
</div>
<div class="block hidden">
<button>btn 2</button>
</div>
<div class="block">
<button>btn 3</button>
</div>
There are no such selector available in CSS to select by their property values. You can try something with jquery by using :hidden selector to find buttons with display:none. Check below snippet for reference.
$( ".btnShow" ).click(function() {
$( ".btn:hidden" ).show( "fast" );
});
.hidden{
display:none;
}
.btnShow{
display:block;
margin-top: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="button 1" class="btn">
<input type="button" value="button 2" class="btn">
<input type="button" value="button 3" class="btn hidden">
<input type="button" value="button 4" class="btn">
<input type="button" value="button 5" class="btn hidden">
<input type="button" value="button 6" class="btn">
<input type="button" value="button 7" class="btn">
<input type="button" value="Show hidden buttons" class="btnShow">
You can check with jquery. The code below means
"Get all buttons, filtered by ones whose parent is visible on page",
loop through and print html of each one.
$(document).ready(function(){
$(":button").filter(function() { return $(this).parent().is(':visible')
}).each(function(){
console.log($(this).html());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="intro" style="display:block">
My name is someone.
<button> a </button> <button> b </button>
</p>
<p>I live somewhere.</p> <p>My best friend is someone.</p>
Who is your favourite:
<ul id="find" style="display:none">
<li>One</li> <li>Two</li> <li><button> x </button>
<button> y </button></li>
</ul>
I see some of the frameworks doing this but it seems to me it is the default. Has anyone any experience with anything other than the default or do they know why it is specified?
Most frameworks let you set their button classes not to button elements only, but other elements like a, div, span etc.
The below illustrates it by using bootstrap btn class name (1)
JS Fiddle
.btn{ margin:5px; }
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="btn btn-primary">This is a div</div><hr>
<a class="btn btn-success"> this is an anchor</a><hr>
<button class="btn btn-warning">Finally, a button</button>
(1) Same thing for Foundation button class and Pure.css pure-button class.