473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 2235
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: timmy_dale12 | last post by:
Hello , im a java programmer whos gotten tangled up in some javascripts. Im really stuck on this one can , can aybody explain this to me. I have a javscript which is to clone a table row and...
6
by: Tim Johnson | last post by:
Hello All: Using javascript to dynamically add row elements to a table. as in ..... row.setAttribute("bgcolor",rowColor); // or cell.setAttribute("bgcolor",rowColor); Using firefox or...
6
by: Pugi! | last post by:
I use mycurrent_cell.setAttribute("style", "background-color:green"); and mycurrent_cell.setAttribute("colspan", "3"); to set some attributes of a table. The code works perfect in FireFox and...
21
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input');...
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
2
by: Aaron Gray | last post by:
Whats going on with setAttribute on IE it appears to work on some examples and working code but not on other code that I am writting ? <style> .foo { font-size: 200%; } </style>
2
by: nygiantswin2005 | last post by:
Hi I have wrote a method in my C# class with the following code, to create an XMl file. The root element is suppose to have 3 attibutes. I can not get attributes to appear correctly. ...
2
by: maminx | last post by:
hello all, i have this script below var td = document.createElement('td'); var theInput = document.createElement('input'); theInput.setAttribute('type', 'text');...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.