0

The output I got from this is suscribe herenoobie. I am trying to add the anchor tag in between the h1

let myElement = document.createElement("h1");
let myAttribute = document.createElement("a");
myAttribute.innerText = "noobie";
myElement.innerText = `suscribe ${myAttribute} here`;
myAttribute.classList.add("decoration");
myAttribute.setAttribute("href", "www.peo.com");
myElement.appendChild(myAttribute);
document.body.appendChild(myElement);

screenshot

dil. 1122
  • 3
  • 2

3 Answers3

1

You are trying to mix some concepts here. The normal way of adding DOM Nodes (that is what you are creating with document.createElement to the DOM is by appending with appendChild.

By using innerText on the other hand, you are just setting the text value for one element. You cannot simply add another DOM element, like ${myAttribute} inside. That is why you don't see it in between.

There are some ways to work around this. 1) If you need to work with just nodes, go all the way with DOM nodes and also create text nodes. For your example, you could create 2 text nodes for the text, and the anchor node, and add all of them to the h1 in proper order, like this:

let myElement = document.createElement("h1");
let textNode1 = document.createTextNode("subscribe "); 
let textNode2 = document.createTextNode(" here");   
let myAttribute = document.createElement("a");
myAttribute.innerText = "noobie";
myAttribute.classList.add("decoration");
myAttribute.setAttribute("href", "www.peo.com");
myElement.appendChild(textNode1);
myElement.appendChild(myAttribute);
myElement.appendChild(textNode2);
document.body.appendChild(myElement);

2) Add the whole h1 content via innerHTML, where you can set HTML values. Now there is not a straight forward way to convert a DOM node to HTML, so you should maybe write HTML directly and not create a DOM node from it (see below for how to create HTML from a DOM node). I don't know about your specific use case to determine what is your preferred option:

let myElement = document.createElement("h1");
myElement.innerHTML = `suscribe <a href="www.peo.com" class="decoration">noobie</a> here`;
document.body.appendChild(myElement);

See https://www.w3schools.com/jsref/prop_html_innerhtml.asp

If you don't want to or cannot write the HTML like that you could get to the HTML of your DOM node by wrapping it in another element and then getting the innerHTML of that wrapper node. You can see how that can be done in the following answer: How to get the HTML for a DOM element in javascript

That way you can still create your link via createElement but you don't need to create text nodes for text. It really depends on your context what is the best option for you.

Matthias S
  • 3,358
  • 3
  • 21
  • 31
  • thanks for your help . writing HTML directly is better but someone told me that this is the best way to learn javascript. – dil. 1122 Jun 15 '20 at 13:49
  • I disagree to be honest. It helps you to understand the concept of the DOM early, that is true, but your progress in creating something is likely very slow. Since you want to keep having fun at learning, in my opinion you should go with a way that keeps you motivated the most by creating nice results. How is your expertise in HTML / CSS so far? What are you trying to do with Javascript now? – Matthias S Jun 15 '20 at 13:53
  • @MatthiasS I guess iam intermediate level in css. Next thing i gotta learn is animation and transitions in css. Along with css i am trying to learn JavaScript so that i could understand frameworks better. I haven't looked into any framework's yet but iam really interested in it – dil. 1122 Jun 16 '20 at 02:56
  • 1
    @RobertoCaboni thanks for mentioning it, yes that is a mistake. I corrected it. – Matthias S Jun 16 '20 at 08:01
  • @dil. I can definitely recommend Vue.js, but I think it is good to get to know Javascript a bit before you get into reactive frameworks (like Vue.js, React or Angular, among others). I think a good challenge is to build some HTML / CSS and then manipulate it with JS, e.g. react to a button click and show / hide parts of the website, for example a menu or a text block. – Matthias S Jun 16 '20 at 08:01
1

let myElement = document.createElement("h1");
let myAttribute = document.createElement("a");
let gebruikersnaam = 'noobie'
myAttribute.innerText = `suscribe here ${gebruikersnaam}`;
myAttribute.classList.add("decoration");
myAttribute.setAttribute("href", "www.peo.com");
myElement.appendChild(myAttribute);
document.body.appendChild(myElement);
I hope this is what you are looking for. If not, let me know!
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
0

You can use
myAttribute.innerHtml="<a href="...">....</a>

You can find more examples of inner html in W3 school here: https://www.w3schools.com/jsref/prop_html_innerhtml.asp

Matt
  • 51
  • 1
  • 7