views:

445

answers:

2

I am modifying the ID of an html div element client side with JavaScript. The following code works OK in Internet Explorer but not in FireFox.

document.getElementById('one').id = 'two';

Can anyone tell me:

a) Why this doesn't work in FireFox.
b) How to make this work in FireFox.

EDIT

To clarify, i'm changing the element ID to reference a different style in an external style sheet. The style is applied in IE but not FF.

EDIT

Turns out the page was been viewed in Firefox/2.0.0.20. So how do i make this work in the old FF version?

Thanks

+4  A: 

It does work in Firefox (including 2.0.0.20). See http://jsbin.com/akili (add /edit to the url to edit):

<p id="one">One</p>
<a href="#" onclick="document.getElementById('one').id = 'two'; return false">Link2</a>

The first click changes the id to "two", the second click errors because the element with id="one" now can't be found!

Perhaps you have another element already with id="two" (FYI you can't have more than one element with the same id).

Roatin Marth
+1 For uploading an example
Seb
+1  A: 

That seems to work for me:

<html>
<head><style>
#monkey {color:blue}
#ape {color:purple}
</style></head>
<body>
<span id="monkey" onclick="changeid()">
fruit
</span>
<script>
function changeid ()
{
var e = document.getElementById("monkey");
e.id = "ape";
}
</script>
</body>
</html>

The expected behaviour is to change the colour of the word "fruit".

Perhaps your document was not fully loaded when you called the routine?

Kinopiko
See my most recent edit.
Tesseract