473,544 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('textb ox');
obj.setAttribut e('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascrip t:void(0)" onClick="replac e();">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('v alue',var) will only work if the input
box has yet to be edited manually?

Jun 5 '06 #1
5 2242
gk****@gmail.co m 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.getEle mentById('textb ox');
obj.setAttribut e('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascrip t:void(0)" onClick="replac e();">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.co m 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.getEle mentById('textb ox');
obj.setAttribut e('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascrip t:void(0)" onClick="replac e();">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.setAttribut e('value','text ')

Jun 6 '06 #4
gk****@gmail.co m wrote:
Can someone explain what I am doing wrong, ... <snip> function replace(){
var obj = document.getEle mentById('textb ox');
obj.setAttribut e('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascrip t:void(0)" onClick="replac e();">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.co m 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.getEle mentById('textb ox');
obj.setAttribut e('value','This is the replaced text');
}
</script>
</head>
<body>
<a href="javascrip t:void(0)" onClick="replac e();">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.setAttribut e('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('v alue',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
15090
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 insert it below the current table row. this is the main part : // New row function. Called by form button. function newRow(baseRowId, tableId) {
6
9803
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 netscape I'm seeing colors rendered as I would hope for. However the same process in Internet Explorer does render the
6
8054
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 Opera, but in IE the table looks like those properties haven't been set. When I use the 'view generated source' (from webdevelopers toolbar in FF) and...
21
39441
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'); input.setAttribute('width', '20em'); I could have just called input.width='20em' When is each better to use, or is there no difference between them?
11
2406
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"> ..historylinks {
2
8839
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
9026
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. XmlDocument document = new XmlDocument();
2
5019
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'); theInput.setAttribute('name', 'codcnourut'); theInput.setAttribute('size', '3'); theInput.setAttribute('value',form.codcnourut.value);...
0
7362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7602
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5291
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3407
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3404
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1837
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
657
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.