Nikolai Aleksandrovsk
Nikolai Aleksandrovsk

Reputation: 39

Html tag with id attribute

I was wondering what html tag that suppport id attribute either than <p> tag because i want to change the tag by javascript but i dont want it to be appear in paragraph. This all what ive been trying

<!DOCTYPE html>
<html>
  <body>
    Name : <p id="user">user1</p>
    <script>
      document.getElementById("user").innerHTML="Arvin";
    </script>
  </body>
</html>

but the result is

Name :
Arvin

what I want is

Name : Arvin

thanks for spending your time to read this..any help will much appreciated

Upvotes: 0

Views: 209

Answers (4)

clement
clement

Reputation: 4266

This code goes wrong because paragraph are shown into a new line (by browser). This code put text in two lines (without your Javascript)

<html>
  <body>
    Name : <p id="user">user1</p>
  </body>
</html>

You maybe shoud better do this:

<html>
  <body>
      <p>Name : <span id="user">user1</span></p>
  </body>
</html>

document.getElementById("user").innerHTML="Arvin";

See running example here:

Upvotes: 1

Emre Acar
Emre Acar

Reputation: 920

Try adding display: inline style in <p> tag and your problem is solved. You can use id calling on any tag though.

<!DOCTYPE html>
<html>
  <body>
    Name : <p id="user" style="display:inline;">user1</p>
    <script>
      document.getElementById("user").innerHTML="Arvin";
    </script>
  </body>
</html>

Upvotes: 0

Paul Roub
Paul Roub

Reputation: 36438

Every tag supports id. In your case, <span> would work well.

document.getElementById("user").innerHTML="Arvin";
Name : <span id="user">user1</span>

Upvotes: 4

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

I cannot think of any tag that wouldn't support the id attribute, neither can the HTML5 spec:

3.2.5 Global attributes

The following attributes are common to and may be specified on all HTML elements (even those not defined in this specification): ... id ...

Upvotes: 0

Related Questions