Referenced input by name?

The field name of my form is contact [0] [state], and I try to reference it through jquery to set the default value, but it does not work, and I wonder if it does not work because of the brackets

I'm trying to:

$('input[name=concat[0][state]]').val('NY'); 
+4
source share
2 answers

Never forget that you are missing quotation marks (").

try the following:

 $('input[name="concat[0][state]"]').val('NY'); 

I know that parentheses will be a problem as an identifier, but as a property, it should work fine while they are in quotation marks.

by adding more information, you can also avoid parentheses, but you should still keep them in quotation marks.

 $('input[name="concat\\[0\\]\\[state\\]"]').val('NY'); 
+12
source

You need to double the brackets:

 $("input[name=concat\\[0\\]\\[state\\]]").val(''); 

Edit: this looks like in jQuery 1.4.4 , but works in 1.4.3 .

+1
source

Source: https://habr.com/ru/post/1337832/


All Articles