How to change id attribute in body tag

  • Thread starter gimme_this_gimme_that
  • Start date
G

gimme_this_gimme_that

I have a page with the following HTML:

<body id="">

What is the Javascript that changes the value of id to "HOME" ?

I'd sort of like to use document.getElementById but the value of id is
''.

Is there a way to get the body tag and assign the value of id from
that?

Thanks.
 
I

Ian Collins

I have a page with the following HTML:

<body id="">

What is the Javascript that changes the value of id to "HOME" ?

I'd sort of like to use document.getElementById but the value of id is
''.

Is there a way to get the body tag and assign the value of id from
that?
document.body.id = whatever;
 
L

-Lost

I have a page with the following HTML:

<body id="">

What is the Javascript that changes the value of id to "HOME" ?

I'd sort of like to use document.getElementById but the value of id is
''.

Is there a way to get the body tag and assign the value of id from
that?

Yes, but why would you want to? There is *no* point using the ID attribute on the BODY
element.

If anything, I would use:

document.getElementsByTagName('body')[0]

*OR*

document.getElementsByTagName('body').item(0)

*OR*

document.body

Just because ID is considered a "standard attribute" does not mean you should use it for
anything.

My $0.02.

-Lost
 
I

Ian Collins

-Lost said:
Yes, but why would you want to? There is *no* point using the ID attribute on the BODY
element.
There is if you want to use an identified element for something...
 
L

-Lost

Ian Collins said:
There is if you want to use an identified element for something...

That made no sense.

Also, having an identifiable element has nothing to do with whether or not the BODY has an
ID attribute.

-Lost
 
I

Ian Collins

-Lost said:
That made no sense.

Also, having an identifiable element has nothing to do with whether or not the BODY has an
ID attribute.
All I'm saying is there can be cases where the body element is just
another element.
 
B

brunascle.maps

I have a page with the following HTML:

<body id="">

What is the Javascript that changes the value of id to "HOME" ?

I'd sort of like to use document.getElementById but the value of id is
''.

Is there a way to get the body tag and assign the value of id from
that?

Thanks.

you probably shouldnt be setting the ID of an element after it's been
loaded into the browser. that should be a hardcoded value that never
changes. that's kind of the purpose of the ID.

and, if you set the ID in javascript, i wouldnt be surprised if
document.getElementById malfunctioned for that element.
 
L

-Lost

you probably shouldnt be setting the ID of an element after it's been
loaded into the browser. that should be a hardcoded value that never
changes. that's kind of the purpose of the ID.

and, if you set the ID in javascript, i wouldnt be surprised if
document.getElementById malfunctioned for that element.

I am not sure why.

<p id="p1" onclick="alert(this.id);">p1</p>

<p id="p2" onclick="document.getElementById('p1').id = 'p4';">p2</p>

<p id="p3" onclick="alert(document.getElementById('p4').innerHTML);">p3</p>

An error fires if you click p2 more than once. Why? Because p1 does not exist after the
first click which made it into p4.

-Lost
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top