I'm working on a new menu, where I have a multiple hidden divs, but I only want to show one div on the page at any one time.
Here is my code; http://jsfiddle.net/sXqnD/
HTML is nice and simple;
<div id="linkwrapper">
<a id="link1" href="#">link1</a><br/>
<a id="link2" href="#">link2</a><br/>
<a id="link3" href="#">link3</a>
</div>
<div id="infocontent">
<div id="link1content">Information about 1.</div>
<div id="link2content">Information about 2.</div>
<div id="link3content">Information about 3.</div>
</div>
Here is my attempt at jQuery, which doesn't seem to play nicely.
$(document).ready(function(){
$('#infocontent').children().hide();
$('#linkwrapper a').click(function(){
var chosen1 = this.id;
$('#infocontent').children('div').each(function(i) {
var i = i+1;
if( $("#" + this.id).is(":visible") == true ) {
$("#" + this.id).hide(function(){
$("#" + chosen1 + "content").show();
});
return false;
} else {
$("#" + this.id).show();
return false;
}
});
});
});
Can anyone see where I have gone wrong or suggest a more elegant solution?
jQuery Effect show() Method The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).
If you want to hide the element when it's empty, look at the CSS :empty pseudoclass in conjunction with display: none. Otherwise you would need to use Javascript to evaluate your conditions and change the CSS value for display or visibility.
The . show() method animates the width, height, and opacity of the matched elements simultaneously. Durations are given in milliseconds; higher values indicate slower animations, not faster ones.
$('div').filter('#idOfDivToShow').show();
$('div').not('#idOfDivToShow').hide();
$('div')
will find all divs on your web page. .filter
will search within the results that match $('div')
and match elements that have id=idOfDivToShow. .not
will search within the results that match $('div')
and match elements that don't have id=idOfDivToShow.
Finally, if you want to search only within a particular element, say #infocontent, then you could write:
$('#infocontent').filter('div').filter('#idOfDivToShow').show();
$('#infocontent').filter('div').not('#idOfDivToShow').hide();
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