Question

<input id="start[2]" type="input" value="10" name="start[2]">

I know the index 2 and I am getting value of this input box using jquery, like this:

str="#start[2]";
result = $(str).val();
alert(result);

but it alerts "undefined".

Was it helpful?

Solution

From jQuery docs:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\

So you need to do:

str="#start\\[2\\]";
result = $(str).val();
alert(result);

Fiddle Demo

OTHER TIPS

You need to escape [ and ] using \

but \ is also special character so you have to escape it also \\

result = $("#start\\[2\\]").val();

Fiddle Demo

you can refer the given fiddle link for this

 <input id="start[2]" type="input" value="10" name="start[2]">

http://jsfiddle.net/GA63Y/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top