1

I need some hepl to find a selector for jQuery.

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 (something like $(input[name=text[]]).each() )

I don't want to use $(input[name^='text']).each(), I don't think it's safe because the rest of my code. Is there a better way?

Can anyone help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is there a parent element you could work with? e.g. if they're all in a div, you could do $("#container input[name^='text'"). – Olly Hodgson Oct 08 '09 at 11:36
  • i can create a container if needed.Never thought of that.Thanks –  Oct 08 '09 at 11:39

4 Answers4

2

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
  • 41,906
  • 4
  • 43
  • 54
2

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
  • 62,884
  • 17
  • 92
  • 129
0

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
  • 524,688
  • 99
  • 697
  • 795
0

Use map() with something like:

$("input[type=text]").map(function() {if ($(this).val() == 1) return $(this)})
powtac
  • 40,542
  • 28
  • 115
  • 170