Reputation: 26271
I have complex input elements of type text
with names like product[1][combinations][x][price]
. There are many of this element, differing in name only by the value after [combinations]
and after [x]
.
For example:
product[1][price]
product[1][combinations][x][price]
product[1][combinations][xx][price]
product[1][combinations][xxx][price]
product[1][combinations][xxxx][price]
product[1][sale_price]
product[1][combinations][x][sale_price]
product[1][combinations][xx][sale_price]
product[1][combinations][xxx][sale_price]
product[1][combinations][xxxx][sale_price]
product[2][price]
product[2][combinations][a][price]
product[2][combinations][aa][price]
product[2][combinations][aaa][price]
product[2][combinations][aaaa][price]
product[2][sale_price]
product[2][combinations][a][sale_price]
product[2][combinations][aa][sale_price]
product[2][combinations][aaa][sale_price]
product[2][combinations][aaaa][sale_price]
the above values x
, xx
, xxx
, xxxx
and a
, aa
, aaa
, aaaa
represent unique values per product[id]
. the first definition in each group(product[2][sale_price]
for example) represents the parent or owner product who's value i will be batch updating to its children(combinations).
I would like to find groups of these elements based on what the type of information is being stored, for example sale_price
and then change its value. I don't need to consider the unique value, [x]
so i hoped i could use a wildcard.
I hoped something like this would solve the problem(example):
$("input[name='product[1][combinations][*][price]'][type='text']").val(0);
however the *
isn't really a wildcard i guess, so i can't use it like that.
I realize i could do something like this, however this will assign 0
to all inputs instead of just sale_price
:
$("input[name^='product[1][combinations]'][type='text']").val(0);
How can i replace this($("input[name='product[1][combinations][*][price]'][type='text']").val(0);
) selector with an appropriate wild card? I'd like to keep the name array values in the same order if possible
Upvotes: 7
Views: 2401
Reputation: 144709
You can use multiple attribute selectors:
$("input[name^='product[" + number + "]'][name$='[sale_price]']").val(0);
Upvotes: 4
Reputation: 36541
not sure if this is what you want... but you can use attribute selecter with $
this selects all the inputs ending with specified text...so that would be
$("input[name$='[price]']").val(0);
$("input[name$='[sale_price]']").val(1);
and you don't actually need [type='text']
the above selector will get the elements with the name ending with the string...however if you want to be more specific and make sure you just need only inputs then its fine..you can add the same here too.
updated
then you can use multiple attribute selector..^
to search the element begining with the specified string
$("input[name^='product[1]'][name$='[price]']").val(0);
$("input[name^='product[1]'][name$='[sale_price]']").val(1);
Upvotes: 7