943,172 Members | Top Members by Rank

Ad:
Aug 29th, 2010
0

jQuery Select From x To y Possible?

Expand Post »
Hi,

I have a list of divs with IDs which are just incrementing numeric values. I want to select a subset of those divs with IDs between say 7 and 12, like this:

$('.myDivs[ID between 7,12]').doSomething();

Anyone know how to do this?

I can't find anything in the jQuery docs and I have Googled the heck out of this and haven't managed to find a solution yet.

Thanks,
Paulo.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FlyByNight73 is offline Offline
5 posts
since Aug 2010
Aug 29th, 2010
0
Re: jQuery Select From x To y Possible?
select the divs whose class contain "myDivs", do an each() and within the function check the numeric suffix of the class
html Syntax (Toggle Plain Text)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  5. <script>
  6. $(function(){
  7. $("div[class*=myDivs]").each(function(){
  8. var id=($(this).attr('class')).match(/(\d+)$/)[0];
  9. if( 7 <= id && id <=12)
  10. {
  11. doSomething( id, this.className );
  12. }
  13. });
  14. });
  15.  
  16. function doSomething(id, cls){
  17. alert( 'class="' + cls + '" with numeric id=' + id);
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="myDivs6">6</div>
  23. <div class="myDivs7">7</div>
  24. <div class="myDivs8">8</div>
  25. <div class="myDivs9">9</div>
  26. <div class="myDivs10">10</div>
  27. <div class="myDivs11">11</div>
  28. <div class="myDivs12">12</div>
  29. <div class="myDivs13">13</div>
  30.  
  31. </body>
  32. </html>
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Sep 8th, 2010
0
Re: jQuery Select From x To y Possible?
Apologies for the tardy response, a last minute decision to take a vacation dragged me away from my desk for a while.

Thanks for the solution Hielo, I modified it very slightly to suit my app and it seems to work great.

Cheers, Paulo!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FlyByNight73 is offline Offline
5 posts
since Aug 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Live Messaging
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Needed: Javascript.help.please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC