51

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/2.0.0.20. It does work in more recent versions of Firefox.

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

Can anyone tell me:

  1. Why this doesn't work in FireFox.
  2. How to make this work in FireFox.

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 in FF.

user229044
  • 232,980
  • 40
  • 330
  • 338
Tesseract
  • 1,547
  • 4
  • 15
  • 16
  • 2
    Should work find in firefox - what makes you think it isn't? – Greg Oct 30 '09 at 14:58
  • 1
    This works for me in Firefox, are you getting a JavaScript error? Can you post more code examples? – Ryan Doherty Oct 30 '09 at 14:58
  • Download Firebug and have a look to see what error is thrown in the console window – Russ Cam Oct 30 '09 at 15:00
  • Works for me on FF 3.5.4, WinXP SP 3, `Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.4) Gecko/20091007 Firefox/3.5.4 (.NET CLR 3.5.30729)` – NickFitz Oct 30 '09 at 15:03
  • When exactly are you doing this? Are you making sure that the DOM is ready first? Try a window.onload handler or put your script after the said element. – Ates Goral Oct 30 '09 at 15:06

3 Answers3

54

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).

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
Roatin Marth
  • 23,589
  • 3
  • 51
  • 55
16

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?

9

You can modify the id without having to use getElementById

Example:

<div id="One" onclick="One.id = 'Two'; return false;">One</div>

You can see it here: http://jsbin.com/elikaj/1/

Tested with Mozilla Firefox 22 and Google Chrome 60.0

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
nimday
  • 333
  • 3
  • 9
  • These days it's (rightfully) considered bad practise to [target elements by there global variables](https://stackoverflow.com/questions/25325221/why-dont-we-just-use-element-ids-as-identifiers-in-javascript). Same goes for [inline event handlers](https://stackoverflow.com/questions/11737873/why-are-inline-event-handler-attributes-a-bad-idea-in-modern-semantic-html). – Mark Baijens Jun 08 '23 at 12:40