The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Missing VALUE attribute in textfield
Christian J
post Jul 26 2011, 07:29 AM
Post #1


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



CODE
<script type="text/javascript">
var input=document.createElement('input');
document.body.insertBefore(input, document.body.firstChild);
input.type='text';
input.id='field';

input.value='foo'; // doesn't work
//input.setAttribute('value','foo'); // does work

alert(document.getElementById('field').value); // returns "foo"
alert(document.getElementById('field').getAttribute('value')); // returns null
document.body.innerHTML+=document.getElementById('field').value; // returns "foo", but form field becomes empty
</script>

When I create a textfield like above it seems to matter if I set the value with

CODE
input.value='foo';

or

CODE
input.setAttribute('value','foo');

With the former, the value appears in the browser window, but when I look at the JS-rendered source the TYPE attribute is missing:

CODE
<INPUT type="text" id="field">

and when I run the last line of the script, the textfield is suddenly emptied in the browser window.

This happens in all my browsers, but only with textfields (not the other INPUT types "button", "hidden", "radio", "checkbox or "submit"). Could it be that

CODE
input.value='foo';

sets a property, as opposed to setting an attribute with setAttribute? That might explain why getAttribute returns null in the former case. But then why does the property value "foo" appear in the textfield, why does this only affect textfields, and why does the innerHTML line make the "value" disappear from the textfield?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 07:58 AM