Sorry guys, I didn't realise the full power of "each" in its incarnation as $.each.

I have therefore achieved my result as follows:

$.each( $("a.open_btn"), function(i, btn) { $(btn).click( function() {
$("#section" + i).addClass("open");
$("#section" + i).slideToggle(1200, complete);
return false;
}); });

A handy snippet of code for doing expandable sections.

Thanks

Steve J



Begin forwarded message:

From: Steve Jones <[EMAIL PROTECTED]>
Date: 19 January 2007 20:12:01 GMT
Subject: Use of "each" to dynamically assign click method and arguments

Hi John / jQuery team

Firstly, this is tremendously exciting stuff. I just moved from the heavyweight bulk of Dojo, and am enjoying myself immensely. This is a last resort, as I have looked everywhere in the documentation to try and find a solution to the following, but can't, and the forums aren't really conduisive to quick discussion yet.

I'm trying to create a simple Ajax FAQ type app. I have four <a> links on a page, each assigned a class "open_button" and I want each one to open a corresponding <div> immediately below it. To differentiate between them, I am using the syntax <div id="section0">, <div id="section1"> etc.

I wondered if it was possible to iterate through these <a class="open_button"> elements using:

$("a.open_button").each(function(i){ //code to add a click method });

BUT instead of setting an actual attribute (like the img example in the documentation for "each" - http://docs.jquery.com/Core#each.28_fn_.29) you set a method, dynamically assigning the target element. So each instance of <a class="open_button"> opens a different <div>.

For a single anchor, the code would be:

$("a.open_button").click(function(){
$("#section0").addClass("open");
$("#section0").slideToggle(1200, function() {
complete(0);
});
return false;
});

BUt obviously I need to dynamically assign 0, 1, 2, 3 to each of the anchors.

Is this possible using "each" or am I barking up the wrong tree?

I will continue to search the docs, as I'm sure you get snowed with questions. At any rate, thanks for a great library... we look forward to developing with it.

Thanks,

Steve Jones












_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to