469,327 Members | 1,262 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

setAttribute has no affect

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?

Jun 5 '06 #1
5 2062
gk****@gmail.com wrote:
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?

--
Ian Collins.
Jun 6 '06 #2
Ian Collins wrote:
gk****@gmail.com wrote:
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?

--
Ian Collins.


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.

Jun 6 '06 #3
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')

Jun 6 '06 #4
gk****@gmail.com wrote:
Can someone explain what I am doing wrong, ... <snip> 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.
Jun 6 '06 #5
gk****@gmail.com wrote:
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 work. I know because I just pasted your text into an HTML
document and tried it. I replaced:
obj.setAttribute('value','This is the replaced text');
with:
obj.value = 'This is the replaced text';
and it worked perfectly in Firefox and IE
Is there a reason setAttribute('value',var) will only work if the input
box has yet to be edited manually?


Yes - you are SETTING the attribute - it is an initial setting of the
value. Once value has been set, you aren't going to set it again...
Jun 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by timmy_dale12 | last post: by
6 posts views Thread by Tim Johnson | last post: by
6 posts views Thread by Pugi! | last post: by
21 posts views Thread by James Black | last post: by
11 posts views Thread by jesdynf | last post: by
2 posts views Thread by Aaron Gray | last post: by
2 posts views Thread by nygiantswin2005 | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.