tags:

views:

49

answers:

4

How can I match something using jquery that is set up as such:

  • id="intersection_resources_4_5" // hit
  • id="intersection_content_6_5" // hit
  • id="intersection_content_4_3" // not

I want to do something like:

$("div:regex(id, intersection_*_*_5)").each(function(){
    $(this).click();
});

Is this doable? Assuming, the * is a wildcard, which I cannot seem to get to work.

A: 

There is regex selector for jQuery:

http://james.padolsey.com/javascript/regex-selector-for-jquery/

That is used like this:

$(':regex(id,^[aeiou])');

Whit
I'm aware of the selector, I just can't seem to make a match for my case :(
Josh
The wildcard character for regex is a single period. Do you mean something like this:$("div:regex(id, intersection_._._5)")
Whit
If you want any integer, use this: [0-9]+
Whit
A: 

Use filter method with custom function. Something like this:

var r = new RegExp("intersection_\d+_\d+_"+num);
$("div").filter(function(){
  return $(this).attr("id").match(r);
}).click();
Anpher
I suck at regex, could you provide me one? Where the * and * can be any integer?var num = 4;"intersection_*_*_"+num
Josh
edited full post :)
Anpher
+1  A: 

With filter, and a proper regex, this should work

$("div:regex(id, ^intersection_\d_\d_5$)").each(function(){
    $(this).click();
});

EDIT:

Try this regex instead ^intersection_[\w-[0-9]]+_\d+_\d+$

You can replace parts between two underscores with the specific word/number if you know what it is.

Chad
I'm sorry, I told you the wrong match :'(Here is what I want to hit:id="intersection_A_B_C"where A is some word, like 'resources', or 'content'where B is some number, 1 - infinitywhere C is some number, 1 - infinity, and is a javascript variable.The id's are like:id="intersection_resources_4_98"id="intersection_content_7_98"id="intersection_resources_8_123"so if the javascript variable was 98, it'd hit the first two.Sorry for the confusion :(
Josh
Here is my current function:$(".bam_header_level").each(function(){ $(this).click(function(){ var header=$(this).attr('id').substr(13); $("div:regex(id, intersection_*_*_"+header+")").each(function(){ $(this).click(); }); }); });
Josh
@Josh, Wrote a new regex that follows the pattern `intersection_word_number_number`, just replace the part you need like you did in your example with a literal where applicable
Chad
+2  A: 

If you don't want a plugin, and you don't want to create your own selector filter, do this:

$("div[id^=intersection]").filter(function() {
    var header = this.id.substr(13); 
    var regex = new RegExp('_[a-zA-Z]+_\\d+_' + header + '$');
    return regex.test(this.id);
}).click();

It would be best if your <div> elements had a class in order to improve performance.

Something like:

$("div.intersection[id^=intersection]")...
patrick dw
This gets me sooo close, but can the word 'resources' be a wildcard string a-z, and can the '98' be the variable 'header'?// code //var header=$(this).attr('id').substr(13); $("div.bam_intersection[id^=intersection]").filter(function() { return /_resources_\d+_98/.test(this.id); }).click();Thanks!
Josh
@Josh - Sure. I'll update.
patrick dw
Works great! Thanks!
Josh
@Josh - You're welcome. :o)
patrick dw