[jQuery] A better way
[jQuery] A better way
I am new to jQuery (coming from Mootools) and the way things are done
here are a bit different, but I am willing to put a lot of effort into
learning it.
That said, I have some working code here (I wrap it in the $
(document).ready function) but I wanted to know if there is a more
efficient way to do it using jQuery's built in functions/selectors. I
am flexible about how it is achieved, but I just need to know how I
should "think" with jQuery.
$("a.showHide").click(function(){
var aRel = $(this).attr("rel");
var dTog = $("div.showHide[id="+aRel+"]");
if(dTog.css('display') == 'block') {
dTog.css('display','none');
} else {
dTog.css('display','block');
}
return false;
});
a basic HTML example of this is:
<a href="#" class="showHide" rel="test">click me</a>
<div class="showHide" id="test">hide me</div>
You can test it yourself, but it just shows or hides the div when the
anchor is clicked. The div ID corresponds to the anchor's REL
attribute.
So in short-- is there a better way?