25

I'm trying to capture value of my input fields and have following method in my form class to do that:

getData: function() {
    var fields = ['name', 'email', 'message'];
    $.each(fields, function(index, el) {
        form.data[el] = $('#'+el).val();
        console.log(form.data[el]);
    });
},

(nevermind the 'select' field, that's a custom made select box and I'll have to handle that one differently) I perform the capture on form change event. The input fields all have proper id's, they look something like this:

<input type="text" id="name">
<input type="email" id="email">
<textarea name="message" id="message"></textarea>

Now, when I type something in the fields I only get value from the textarea, while other inputs give me undefined.

I'll also note that the form is being loaded with Ajax, but since it captures the change event on the fields normally I doubt that this is the problem.

3
  • 6
    confirm that you don't have any other elements with duplicate id causing issues - because that should work. Commented Jan 28, 2014 at 13:36
  • yes ofcourse , form.data[el] = $('#'+el).val(); this statement needs actually maps via name attr. anyway close the question with self answer. Commented Jan 28, 2014 at 13:41
  • To add to @NickDickinson-Wilde - I just had an instance where there was existing logic updating the input field via jQuery. This is probably a good thing to check as well, once I placed the new .val() logic after the existing logic it started working. Commented Aug 21, 2023 at 18:56

5 Answers 5

42

I have figured it out.

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

Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me as well, I would appreciate it if someone could explain why though.
According to W3C recommendations, an html for can only send data if the input field has a control name. Ids do not count. https://www.w3.org/TR/html401/interact/forms.html#successful-controls
11

"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!

Comments

4

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

1 Comment

Perhaps the behaviour of .val() has changed in the recent jQuery versions.
1

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">

Comments

1

the most stupid debugging I have ever had. After 3 days of looking for issues, I found that a link to jquery lib is missing. Unbelievable :D

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.