-1

My css 3 tooltip (created folowing this tutorial) is showing this :

Name </br> Description

instead of

Name
Description

The html code used to create the tooltip has been made by my javascript script :

var DivItem = document.createElement("div");
var span = document.createElement("span");
var spanText = document.createTextNode(Name + "<br>" + Description;

span.appendChild(spanText);
span.className = "TooltipText";

DivItem.appendChild(span);
DivItem.className = "Tooltip";

MainDiv.appendChild(DivItem);

I need to use the javascript code here beacause i make an list of items (got from a json), and i need to have a tooltip to show more informations on the hovered item.

KenAcros
  • 3
  • 1

4 Answers4

1

createTextNode, unsurprisingly, creates a text node.

If you want a br element, you need to create an element.

var span = document.createElement("span");
span.appendChild(document.createTextNode(Name));
span.appendChild(document.createElement("br"));
span.appendChild(document.createTextNode(Description));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Instead of createTextNode you can use:

span.innerHTML = `${Name}<br>${Description}`
emanuelbsilva
  • 252
  • 3
  • 10
0

Add with innerHTML of created element.You can achieve that reference

var DivItem = document.createElement("div");
    var span = document.createElement("span");
    span.innerHTML =Name + "<br>" + Description;
    span.className = "TooltipText";

    DivItem.appendChild(span);
    DivItem.className = "Tooltip";

    MainDiv.appendChild(DivItem);
Community
  • 1
  • 1
prasanth
  • 22,145
  • 4
  • 29
  • 53
-1

you can not use text node to render html.instead you can use innerHTML

Dnyanesh
  • 51
  • 6