SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I iterate through a set of elements? (simple jQuery question)

    Hi,

    I basically have a set of elements in my page that share the same 'class', but need to be processed individually depending on their 'id' attribute like:
    Code:
    <div class="item" id="4345"></div>
    <div class="item" id="14"></div>
    <div class="item" id="566"></div>
    What I'd like to know is how to iterate through the found elements to process them one by one. (I'm new to jQuery)

    Any ideas?


    Appreciate your help

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it not something like

    Code javascript:
    $('.item').each( function() {
    // do something with the item
    if($(this).id == '4345')
    alert('hey');
    });


  3. #3
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really. The list of items I have change constantly (so I deal with different id's).

    The script takes every item's id, sends it to a script (ajax) and inserts the result back into the item again.

    I just don't know how to iterate items.

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    $('.item').each(function(){
      var id = this.id;
      ajaxFunctionExample(id);
    });
    gRoberts' script was spot on.
    To reference an individual iteration just use the "this" keyword...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •