Duddy67
Duddy67

Reputation: 1046

Empty value after form submit

I'm facing a very weird problem regarding the dynamical setting of an input value with JQuery.

The initial state of the targeted input is as following:

<input type="text" id="birthdate" name="birthdate" value="" data-alt-value="0000-00-00" autocomplete="off" data-local-value="0000-00-00">

Note: The input fields are generated through the Joomla framework (may be it matters, I don't know).

If I set the value in a regular way:

$('[name="birthdate"]').val("1986-01-21");

I get this result:

<input type="text" id="birthdate" name="birthdate" value="" data-alt-value="1986-01-21" autocomplete="off" data-local-value="1986-01-21">

The value attribute is not set. So I tried this way:

$('[name="birthdate"]').attr('value', "1986-01-21");

which gives the expected result:

<input type="text" id="birthdate" name="birthdate" value="1986-01-21" data-alt-value="1986-01-21" autocomplete="off" data-local-value="1986-01-21">

But for whatever reason the value is empty after the form is submitted. Even stranger, if I give the focus to the birthdate field (by clicking in it) then submit the form, the value is passed correctly.

I would point out that this problem occurs only with input fields dealing with date or datetime values.

Can someone helps me ?

Upvotes: 0

Views: 1258

Answers (1)

Barmar
Barmar

Reputation: 780899

When a form is submitted, the parameters that are sent come from the value properties of the input fields. The value attribute is used as the initial value of the value property, but changing the attribute doesn't change the property (the attribute is saved separately so that the form's reset() method can restore it).

To change what gets submitted, you just need to change the property, not the attribute. You won't see this change when you look at the DOM in the console.

See Why should HTML DOM properties be reflected into HTML DOM attributes as well? for some more information about the relationship between attributes and properties. Some properties and attributes work a little differently from others.

Upvotes: 2

Related Questions