0

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.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by FlyByNight73
0

select the divs whose class contain "myDivs", do an each() and within the function check the numeric suffix of the class

<!DOCTYPE html>
<html>
<head>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
	<script>
	$(function(){
		$("div[class*=myDivs]").each(function(){
			var id=($(this).attr('class')).match(/(\d+)$/)[0];
			if( 7 <= id && id <=12)
			{
				doSomething( id, this.className );
			}
		});
	});
	
	function doSomething(id, cls){
		alert( 'class="' + cls + '" with numeric id=' + id);
	}
	</script>
</head>
<body>
	<div class="myDivs6">6</div>
	<div class="myDivs7">7</div>
	<div class="myDivs8">8</div>
	<div class="myDivs9">9</div>
	<div class="myDivs10">10</div>
	<div class="myDivs11">11</div>
	<div class="myDivs12">12</div>
	<div class="myDivs13">13</div>

</body>
</html>
0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.