I have the following input field
<input type="text" name="location[state_id]" id="location[state_id]" value="" >
and i am trying to assign there a value with the following jquery
$("#location\\[state_id\\]").val("5");
but it does not working! It works only if the name of both is only location.
Thanks
The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element.
As #
is an id selector, you could try to set the id of the input to a valid CSS id and let the name of the input the same (I assume that this format is required by a framework of some kind). For example, the HTML can look like:
<input type="text" id="location_state_id" name="location[state_id]" value="" />
and the jQuery expression:
$('#location_state_id').val('5');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With