Assigning value to form using .val()
forms, javascript, jquery
Solution
The code works because even though `HTMLFormElement` instances aren't defined to have a `value` property, `val` doesn't check, it just happily assigns to it, creating an expando. This works with any element:
var d = $("<div>");
d.val("foo");
console.log(d.val()); // "foo"
console.log(d[0].value); // "foo"
Submitting a form with a `value` expando won't include that value with the submission (you can try it here), so I'd expect if the code is doing this, it's doing it purely for client-side reasons. Or, of course, it's a bug they've never caught and fixed. (Or they might monkey-patch `val` and make it do something special.)
Problem
While going through some 3rd party code I realised that they are assigning a value to the form using .val() and then submitting the form. ``` $('#form-ID').val("Some Value"); ``` This is a valid jquery statement and if you do ``` $('#form-ID').val() ``` you get `"Some Value"` on the console but no HTML change is observed to reflect the "Some Value". So if there is no HTML change then: Why assign a value to the form ? If it is required how will the PHP code read this value?