setAttribute has no affect

G

gkelly

Can someone explain what I am doing wrong, or why this will not work?
I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same
result.

Take for example this code:

<html>
<head>
<script>
function replace(){
var obj = document.getElementById('textbox');
obj.setAttribute('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascript:void(0)" onClick="replace();">replace</a>
<input type="text" id="textbox" name="textbox" value="">
</body>
</html>


If you load this in a webpage and click on 'replace', the input box
will be filled with "This is the replaced text", as expected. Now click
in the input box and delete a few letters, or the entire text, and
subsequent clicks on 'replace' will have no affect to the value in the
input box.

I've tried using obj.value = 'This is the replaced text', but this
method doesn't work in Mozilla.

Is there a reason setAttribute('value',var) will only work if the input
box has yet to be edited manually?
 
I

Ian Collins

Can someone explain what I am doing wrong, or why this will not work?
I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same
result.

Take for example this code:

<html>
<head>
<script>
function replace(){
var obj = document.getElementById('textbox');
obj.setAttribute('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascript:void(0)" onClick="replace();">replace</a>
<input type="text" id="textbox" name="textbox" value="">
</body>
</html>


If you load this in a webpage and click on 'replace', the input box
will be filled with "This is the replaced text", as expected. Now click
in the input box and delete a few letters, or the entire text, and
subsequent clicks on 'replace' will have no affect to the value in the
input box.

I've tried using obj.value = 'This is the replaced text', but this
method doesn't work in Mozilla.
Yes it does, so you must be doing something else wrong. It's probably
the preferred way due to setAttribute not working expected in IE>

Why complicate things with an anchor, have you tried a button?
 
G

gkelly

Ian said:
Yes it does, so you must be doing something else wrong. It's probably
the preferred way due to setAttribute not working expected in IE>

Why complicate things with an anchor, have you tried a button?

I'm still not having any luck. I've stripped down the example to the
bare necessities, so I hope there isn't something else that's wrong. Is
there something I'm leaving out?

Have a look at http://gmk.sytes.net/test/js.html

Using the button doesn't seem to make a difference.
 
G

gkelly

OK, I am now using obj.value = 'text'. Mozilla seems to have a problem
only if you use this method AND obj.setAttribute('value','text')
 
R

Richard Cornford

Can someone explain what I am doing wrong, ...
function replace(){
var obj = document.getElementById('textbox');
obj.setAttribute('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascript:void(0)" onClick="replace();">replace</a>
<input type="text" id="textbox" name="textbox" value="">
</body>
</html>


If you load this in a webpage and click on 'replace', the
input box will be filled with "This is the replaced text",
as expected. Now click in the input box and delete a few
letters, or the entire text, and subsequent clicks on
'replace' will have no affect to the value in the input
box.
<snip>

Where fully supported, changing the value of the 'value' attribute at
runtime should update the - defaultValue - property of the corresponding
element. Changes to the - defaultValue - property at runtime would not
be expected to update he displayed text.

See the W3C HTML Level 2 DOM specification for the relationship between
the 'value' attribute and the - value - and defaultValue - properties of
elements, and set the - value - property at runtime to update the
displayed text.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top