0

I have a <form> with this input...

Term ID: <input type="text" name="data_array[0][term_id]" size="5" value="' . $highest_term_id . '">

where $highest_term_id is set by PHP.

I'm trying to use jquery to increment the "data_array[0]" part by...

$("input[data_array[" + index + "][term_id]").val((index + 1));

but I get this error:

Error: Syntax error, unrecognized expression: input[data_array[undefined][term_id]"

A bit higher in the script I have var index = 0;;

Any ideas why this is failing?

1 Answer 1

1

The square bracket has special meaning in jQuery selectors - you'll have to escape them - should be something like:

$("input[name=data_array\\[" + index + "\\]\\[term_id\\]]").val((index + 1));

I'd try to avoid this naming convention if possible, or try to find an alternative to selecting it by name - as you can see, it's hard to keep the brackets and backslashes straight when referencing it.

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

6 Comments

The error is gone now, but the increment part isn't working. Thanks for the info about the [ ]s though.
When you say "increment", you're not incrementing anything here - you're just saying index + 1. Are you trying to do ++index?
Sorry... yeah. A bit later in the code I do index++;.
Tried $("input[data_array\\[0\\]\\[term_id\\]").val(); in Firebug (Firefox) and it's returning null.
The selector is still wrong - the data_array[0][term_id] is the name of the tag, so your selector should be looking at name. Look at my example again.
|

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.