2

I want to send a value to the form field with the jQuery. Here's the basic code, it will work fine:

<input id="logo" name="logo" type="text" />

jQuery:

jQuery('#logo').val(imgurl);

However, I need to change the field ID to option[logo]. So I am trying this:

<input id="option[logo]" name="option[logo]" type="text" />

jQuery:

jQuery('#option[logo]').val(imgurl);

This does not work. How this can be fixed?

user1355300
  • 4,867
  • 18
  • 47
  • 71
  • related to this: http://stackoverflow.com/questions/2364982/jquery-selector-for-inputs-with-square-brackets-in-the-name-attribute – VDP Jul 31 '12 at 06:48

3 Answers3

4

You can't just use [ or ] literally as characters in an attribute/element name in a JQuery selector because they have a special meaning. You have to escape them. Read the documentation here: http://api.jquery.com/category/selectors/

This should work:

jQuery('#option\\[logo\\]').val(imgurl);
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
2

add a class to that input and try with below

<input id="logo" class="option" name="option[logo]" type="text" />

jQuery('input.option').attr({
                             "id": "option\[logo\]",
                             "name": "option\[logo\]"
                            });

Working DEMO

xkeshav
  • 53,360
  • 44
  • 177
  • 245
2

You need to escape the brackets in the jQuery like this:

$('#option\\[logo\\]').val(imgurl); 
Jory Cunningham
  • 744
  • 5
  • 6