-3

i would like to ask how i can work with html tags e.g. <h1> or <b> in JavaScript?

My Code :

document.addEventListener('DOMContentLoaded', function() {
  if (window.location.href.indexOf("/login?from=") !== -1) { 
    var hint=document.createTextNode("<b>Hello</b>"); 
    var node = document.getElementById("main-panel"); 
    node.insertBefore(hint, node.firstChild); 
  }
})

But they show the text string <b> hello </b> and does not make it big .

i can only work in a js file i dont have the opportunities to work with div tags in html file

1
  • Because you insert it as text. Try innerHTML instead OR document.createElement("b") and appendChild Commented Jun 6, 2018 at 11:11

2 Answers 2

5

First create an element b and the add the desired text to it.

var bold = document.createElement('b')
bold.innerHTML = "hello"
var parent = document.getElementById("haha");
parent.appendChild(bold)
<div id="haha">put the text here
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

The Problem i only have an js file and can work in the JS File i dont have an html file where i can paste the div tag.
@M.Max: Aren't you fetching the node from HTML here? : var node = document.getElementById("main-panel"); you do have an element with main-panel id.
-1

You can use this script:

        <script>
        document.addEventListener('DOMContentLoaded', function() {
        if (window.location.href.indexOf("/login?from=") !== -1) { 
           var hint = '<b>Hello</b>';
           document.write(lines);
           var node = document.getElementById("main-panel"); 
           node.insertBefore(hint, node.firstChild); 
       }
       })   
       </script>

I tested and it worked.

2 Comments

How i can use this one in my example because if i change the line var hint=document to var lines they show me only the messsage and not the requested page
I change my answer above, please check.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.