Hi there,

Couldn't you just do something like this?

  $("a.open_button").each(function(index) {
    $(this).click(function() {
$('#section' + index).addClass('open').slideToggle (1200,function() {
        complete(index);
      });
      return false;
    });
  });



--Karl
_________________
Karl Swedberg
www.englishrules.com
www.learningjquery.com



On Jan 19, 2007, at 3:48 PM, Steve Jones 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


<new-logo-small.gif>








<new-logo-small.gif>


<new-logo-small.gif>
<new-logo-small.gif>
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to