Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to toggle two divs in the same area

Tags:

html

jquery

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?

like image 794
leora Avatar asked Oct 31 '10 16:10

leora


People also ask

How do I toggle between two divs?

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.

How do I make two divs display on the same line?

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.

How do I align two divs side by side horizontally?

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.


1 Answers

By using the toggle() function:

$("#link").toggle(function() {
  toggleDivs();
  $(this).html("Show viewA");
}, function() {
  toggleDivs();
  $(this).html("Show viewB");
});

function toggleDivs() {
  $("#divA, #divB").toggle();
}
like image 183
PeterWong Avatar answered Nov 14 '22 02:11

PeterWong