Steve,

This brings to mind one of my mottos, "IDs are a crutch for the weak.
Classes are for the strong."

a simple way of keeping the button and div hooked together is to give them a
common class
<a class="open_btn section1"....> with the <div class="closed section1">
the only tricky part is to isolate the second class from the <a>, but it's
easier than the gyrations you've gone through!


On 1/19/07, Steve Jones <[EMAIL PROTECTED]> wrote:

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
*To: [EMAIL PROTECTED]
*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
[email protected]
http://jquery.com/discuss/






--
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to