i am loading two divs in a page: divA and divB
i have divB display style = none.
I have a link that says "Show viewB". When i click this i want div B to show up where divA is and divA to hide.
i then want the link to change to "Show viewA"
what is the most elegant way of doing this in jquery?
Add a class show to the two div and then you can use a toggle method. example http://jsfiddle.net/WSCBu/21/ shown here. Show activity on this post.
You can use display: inline to put the two div elements inline. Explanation: div elements are block elements, so their display style is usually display: block . You can wrap both the div elements in a span tag. Explanation: span works the same way as the div , to organize and group elements.
To position the divs side by side, we are using the float property to float each . float-child element to the left. Since they are both floating to the left, they will display side by side if there's enough space for both to fit.
By using the toggle()
function:
$("#link").toggle(function() {
toggleDivs();
$(this).html("Show viewA");
}, function() {
toggleDivs();
$(this).html("Show viewB");
});
function toggleDivs() {
$("#divA, #divB").toggle();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With