Simon.
Simon.

Reputation: 1896

How to write out HTML from ajax call using JS

This might seem a little simple, but i've tried many ways & non of them are working as expected.

i have values coming in from an ajax call, & i am displaying these to a <table>. the data will not be seen at first (css - display:none) but onclick involves a function which displays a dialog of said data.

writing out the data in these ways does not work:

var text = "Example Data<br>";

var text = document.createTextNode("Example Data" + document.createElement('br'));

var text = document.createTextNode("Example Data");
    text += document.createElement('br');

The latter outputs [object Text][object HTMLBRElement]

How do i write this correctly??

Upvotes: 0

Views: 1136

Answers (4)

Adil Abbas
Adil Abbas

Reputation: 73

You can try this:

  1. Give the table an id
  2. Append html response to the table by using $('#tableid').html(responsedata);

Upvotes: 0

Florian Gl
Florian Gl

Reputation: 6014

Try

var p = document.createElement("p");
p.innerHTML = "Example Text<br>";

Upvotes: 0

Garrett
Garrett

Reputation: 1688

You need to append the line break
as an HTML element "createElement" as it is an HTML element.

var text = 'test';
var newtext = document.createTextNode(text),
    p1 = document.getElementById("p1");

p1.appendChild(newtext);
p1.appendChild(document.createElement('br'));
p1.appendChild(document.createTextNode('newline displayed'));

Upvotes: 0

Quentin
Quentin

Reputation: 944568

You can't concatenate node objects (trying to do so with + will convert them to strings first).

Find the element you want to append the nodes you've created, and call appendChild on it repeatedly.

var text = document.createTextNode("Example Data");
someElement.appendChild(text);
someElement.appendChild(document.createElement('br'));

Upvotes: 1

Related Questions