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.

Recommended Answers

All 2 Replies

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>

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.