Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery assign value to input

Tags:

jquery

input

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

like image 724
George Pittas Avatar asked Oct 14 '12 20:10

George Pittas


People also ask

How do I set the value of a element in jQuery?

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.

How can get input tag value in jQuery?

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.


1 Answers

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');
like image 136
npclaudiu Avatar answered Oct 10 '22 04:10

npclaudiu