I have 2 divs. I want div id 1 to be visible when the page is loaded and when i click on anywhere in main div id 2 should be visible.when user click again on main 2 should be hidden and 1 visible.`
<div id="main" style="border:1 px dashed red;" >
<div id="1" style="width:300px;height:50px; ">div One visible</div>
<div id="2" style="width:300px;height:50px; ">div two visible</div>
</div>
How do i get it done using Jquery? i am not sure how to get the toggling effect
Thanks
Easy peasy.
jsfiddle demo →
Edit
OP Comment: "Is there anyway i can keep track of which div is visible?"
There certainly is; it depends on how you want to use it. You can manually maintain a flag:
Or you can just write a function that figures it out for you:
As jAndy points out, this function can be written in a more concise/terse form:
However, it is semantically different from the first version. The first version returns one of:
$1$2undefinedwhile jAndy’s version returns one of:
$1$2$1.add($2), a two-element jQuery object$(), an empty jQuery objectSo they’re not strictly equivalent.