2

I have an input for which I set the value after an AJAX request, but when I submit the form the value of this input is empty

Here's my input :

<input type="hidden" value="" name="NbEmprunteurs" id="NbEmprunteurs"/>

and here's how I set in the callback function :

$('#NbEmprunteurs').attr('value',data.nbEmprunteurs);

on submit i get all the other inputs but this one, any idea why ?

95faf8e76605e973
  • 13,643
  • 3
  • 24
  • 51
Jihane
  • 135
  • 12

1 Answers1

4

Use .val() to set the value.

$('#NbEmprunteurs').val(data.nbEmprunteurs);

Reference: http://api.jquery.com/val/

.attr() doesn't work because it changes the HTML element attribute but not the current state. Attributes are key-value pairs that you type in HTML tags <input ... > and state is an information about the DOM element stored inside JS engine. In some cases the HTML element state can differ from the attribute values. For example, input's value: when you type something in an input, its value attribute isn't changed (you can see it in a browser dev tool).

Finesse
  • 9,793
  • 7
  • 62
  • 92