jQuery .val() not working for input fields

I have figured it out.

I was missing the name attribute on my input fields and for some reason .val() needed that to work.


"According to W3C recommendations, an html for can only send data if the input field has a control name. Ids do not count." Well, that is all fine and good (priceless, in fact) but jQuery .val() would have one believe it can happily use the id= as a selector if you look at this example. My testing showed not only does one have to have id= and name= but they have to be the same. I so love this stuff when it works!


Thanks @nick-dickinson-wilde for your comment. I had the same problem. I wanted to get the value of the following input.

<input id="ipValue" class="form-control" type="text" value="@Model.IPValue" />

I was surprised the following did not work.

javascript var ipValue = $("#ipValue").val();

I didn't notice I had an element with the same id <div class="col-3" id="ipValue">


No, You do not have to have both name and id attribute for val to work. I had test it, with only id , .val() works fine