Reputation: 102439
I am using the following code to write the table. Now I wish to add subscript text after the table text. How can it be achieved?
My code has:
oCell = document.createElement("TD");
oCell.innerHTML = data;
oRow.appendChild(oCell);
How do I add a subscript text followed by the data?
Upvotes: 0
Views: 1636
Reputation: 5728
You may mean this:
oCell = document.createElement("TD");
oSub = document.createElement("sub");
oSub.innerHTML = data;
oCell.appendChild(oSub);
oRow.appendChild(oCell);
Upvotes: 0
Reputation: 42140
Just carry on with the innerHTML you're using:
oCell.innerHTML = data+'<br /><sub>'+subText+'</sub>';
Or even
oCell.innerHTML = data+'<br />'+subText.sub();
for some JavaScript 1.0, retro good?
Upvotes: 1
Reputation: 536735
oCell.innerHTML = data;
If 'data' can contain '<' or '&' characters, you've just done a blunder. Instead, use createTextNode to put plain text into HTML:
oCell.appendChild(document.createTextNode(data));
oCell.appendChild(document.createElement('sub')).appendChild(document.createTextNode(subdata));
Upvotes: 0
Reputation: 655785
You can use the following to append another element to the td
element:
var newElem = document.createElement("sub");
newElem.appendChild(document.createTextNode("foobar"));
oCell.appendChild(newElem);
Upvotes: 3