Forum Moderators: open

Message Too Old, No Replies

Change the ID of a tag with javascript

can it be done...?

         

mipapage

9:42 pm on Dec 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I have <body id="style1">

how can I select the body tag and change the id to, for example, style2?

moonbather

1:06 am on Dec 18, 2003 (gmt 0)

10+ Year Member



Could you let me know if this works for you.


<head><style type="text/css">
#style1 {background-color:yellow}
#style2 {background-color:pink}
</style>
<script>
<!--
function newid(){
document.getElementsByTagName('body')[0].id="style2"}
// -->
</script></head><body id="style1">
If I have &lt;body id="style1"&gt;<br />
<a href="javascript:newid()">how</a> can I select the body tag and change the id to, for example, style2? <br />
</body>

mipapage

1:17 am on Dec 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



moonbather,

supercool, thanks! What I tried was sticking

document.getElementsByTagName('body')[0].id="style2"

into an onclick. It seems that without the quotes around style2 it fails.



<head><style type="text/css">  
#style1 {background-color:yellow}
#style2 {background-color:pink}
#style1 p{width:300px;height:50px;border:2px solid
#000;position:absolute;top:60px;left:100px}
#style2 p{width:100px;border:5px solid #000;position:absolute;top:150px;left:300px}
</style>
<script>
<!--
function newid(){
document.getElementsByTagName('body')[0].id="style2"}
// -->
</script></head><body id="style1">
If I have &lt;body id="style1"&gt;<br />
<a href="#" onclick="javascript:newid()">how</a> can I select the body tag and change the

id to, for example, style2? <br />
<p>ioend</p>
</body>

Cool stuff, that javascript.