7

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

2 Answers 2

7

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.

example fiddle here

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);

updated fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

almost thought this would do it for me... except that when targeting the END of the name, you lose the product[1] selector which is needed to limit which values are updated(per product)
welcome.. no problem..@undefined beats me most of the time.. hehe :p.. the internet went down for some time so was late.. anyways glad this helped..happy coding...
Although this is not the accepted answer but here I know what is $ and ^ for... thanks...
4

You can use multiple attribute selectors:

$("input[name^='product[" + number + "]'][name$='[sale_price]']").val(0);

http://jsfiddle.net/QsVnR/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.