Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript and defaultValue of hidden input elements

Tags:

javascript

dom

Assume you have input element:

<input id="aaa" type="text" value="unchanged" />

Then launch js script:

var e = document.getElementById("aaa");
e.value = "changed";
alert(e.defaultValue + "/" + e.value);

Result will be "unchanged/changed". Unfortunately, when your input element is hidden:

<input id="aaa" type="hidden" value="unchanged" />

...the same js script seem not to be working any more. Result is "changed/changed". Is this a proper way? If so, why only hidden form elements act different?

like image 829
wst Avatar asked Mar 16 '11 00:03

wst


People also ask

How do you make the input field not visible?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.

Is input type hidden safe?

Since they are not rendered visible, hidden inputs are sometimes erroneously perceived as safe. But similar to session cookies, hidden form inputs store the software's state information client-side, instead of server-side. This makes it vulnerable.

How do you get hidden field values in typescript?

var h = document. getElementById('myHiddenField');

How do I hide hidden fields in inspect element?

You Cannot. Debuggers are designed for debugging HTML and Javascript. But you can try disabling Right click. Another way is that you can use sessions to store user variable so it can be accessed only on server side.


2 Answers

The "defaultValue" property is only maintained in a way you apparently expect for "text", "file", and "password" fields.

Here is the relevant portion of the DOM spec.

I suspect the reason for this is that user activity on its own cannot change the value of hidden elements. If you want to preserve the initial values, run something at "load" or "ready" to stash the value somewhere.

like image 80
Pointy Avatar answered Sep 22 '22 17:09

Pointy


For hidden input elements, defaultValue isn't actually implemented. The reason why you get the same result ast .value is because the browser your using is just defaulting.

See here for a discussion of this with Firefox.

like image 33
Mike Lewis Avatar answered Sep 26 '22 17:09

Mike Lewis