views:

84

answers:

4

Hi i need some hepl to find a selector for jqeury

I have these textboxes

<input type="text" name="text[1]" value="1,2,3">
<input type="text" name="text[2]" value="1,8,9">
<input type="text" name="text[3]" value="7,4,3">

i need for each these textboxes do a search and find if value=1 exist. I need help to the selector (somthing like $(input[name=text[]]).each()

i dont want to use $(input[name^='text']).each(), i dont think its safe because the rest of my code.Is there a better way.

Can any one help

A: 

Why not do:

$('input[name="^text["]').filter('[name$="]"]')

I don't know if you'll be able to make it any more specific than that with out using a function for the filter. I'd probably be ok with the just name starts with text and make sure to use different ids elsewhere.

tvanfosson
Thank.I have not use filter before.So i will test drive it
ntan
+1  A: 

I'd use a placeholder class thus:

<input class="myselector" type="text" name="text[1]" value="1,2,3">
<input class="myselector" type="text" name="text[2]" value="1,8,9">
<input class="myselector" type="text" name="text[3]" value="7,4,3">

Then it's just a matter of:

$(input.myselector).each()
Lazarus
A: 

Use map() with something like:

$("input[type=text]").map(function() {if ($(this).val() == 1) return $(this)})
powtac
A: 

Wrap them in a container:

<div id="container">
 <input type="text" name="text[1]" value="1,2,3">
 <input type="text" name="text[2]" value="1,8,9">
 <input type="text" name="text[3]" value="7,4,3">
</div>

And select them with it, like this.

$("#container > input").each(function(i, element){
    $element = $(element);
    if($element.val().indexOf('1') >= 0){
        $element.css("background-color", "yellow");
    }
});

Or even write a custom selector like this:

$.extend($.expr[':'], {
    contains1: function(e) {
        return $(e).val().indexOf('1') >= 0; 
    }
});

$("#container > input:contains1").css("background-color", "yellow");
Tim Büthe
I like the customer selector example... +1
Lazarus
Yeah, custom selector are pretty slick. Check out http://stackoverflow.com/questions/182630/jquery-tips-and-tricks for more tips and tricks
Tim Büthe