Daniel Garcia Sanchez
Daniel Garcia Sanchez

Reputation: 2344

jquery - select dom by name like

I have a lot of doms with these names:

<input id="partido1-jugada4-empate" name="bets[3][0][0]" title="partido1-jugada4-empate" type="radio" value="X">

<input id="partido1-jugada4-empate" name="bets[3][1][0]" title="partido1-jugada4-empate" type="radio" value="X">

...

<input id="partido1-jugada4-empate" name="bets[3][13][0]" title="partido1-jugada4-empate" type="radio" value="X">

I need select these with something like:

$( "input[name*='[3][*][0]']" )

where previous line give some input with that name

Upvotes: 2

Views: 152

Answers (2)

Vect0rZ
Vect0rZ

Reputation: 401

As this question here suggests

jQuery selector for matching at start AND end of ID

You should do the following:

$("input[name ^=//[13//]][name $=//[0//]")

So it reads if the name starts with [13] and ends with [0]

The backslashes '//' are used to tell jQuery that '[' and ']' are not it's reserved brackets.

Edit:

The proper answer is thanks to:

ᾠῗᵲᄐᶌ

In the comments below :)

Upvotes: 2

wirey00
wirey00

Reputation: 33661

You can use .filter() and use a regex to compare the name to see if it matches a certain pattern

var x = $('input').filter(function(i,v){
    return /^bets\[3\]\[.+\]\[0\]$/.test(v.name);   
});

Fiddle

^bets\[3\]\[.+\]\[0\]$

Regular expression visualization

Debuggex Demo

Upvotes: 1

Related Questions