views:

210

answers:

6

Sorry if this question is mind numbingly easy to answer, but I'm a bit new to JQuery and I have a tight deadline.

I am looking for a selector for textbox elements that have this format:

id = "FixedName_#"

"FixedName" will always be "FixedName", but I only want to find elements where the # is positive. So I would want to find "FixedName_1", and "FixedName_2" for example, but skip over "FixedName_-1" and "FixedName_-2".

Thanks!

Update Ended up going with something like this (modified my actual code for display here so not sure if this works as shown):

$("input[id*='FixedName_']").each(function() {
    if ($(this).attr("id").charAt(10) == "-") {
        //Do something.
    } else{
        //Do something else.
    }
});
+1  A: 

This might be of some help

Regex Selector for jQuery

astander
+1  A: 

After adding this attribute selector, you can do this:

$("*:regex(id, ^FixedName_\\d+$)")

Will also match FixedName_0.

John Calsbeek
+2  A: 

I'm not sure you can do that with one selector. What I'd do is select all the elements that have an ID starting with FixedName_ like this:

$("[id|=FixedName_#]")

Then you can loop through the results and examine the value after the #.

EDIT: Try this:

$("[id^=FixedName_#]").each(function(){

var controlNum = parseInt(this.id.replace('FixedName_#',''));
  if (controlNum >= 0){
    //......
  }
});
Kevin Tighe
I like this solution the best so far since I wouldn't have to include extra regex code. Can you use selectors on selectors instead of looping through the results? For example, I would have a variable that collects all "FixedName_" elements, then run a selector on that variable to select only those with "-" characters?
Ocelot20
A: 

You could try adding a class of "FixedName" and then adding a rel of the number so you could use this: if ( $('.FixedName').attr('rel') > 0 ) { ... }

Aaron Mc Adam
+1  A: 

This stored the elements that their id does not contain a - sign

var elems = $('input:not([id*=-])');

alert(elems.size());
XGreen
A: 

Is there any chance the code that generates the textboxes could set class attributes like class="positive/negative" based on the id value? To be able to select $('.positive')...

DShultz