venkatachalam
venkatachalam

Reputation: 102439

Subscript text in HTML

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

Answers (4)

Norbert B.
Norbert B.

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

meouw
meouw

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

bobince
bobince

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

Gumbo
Gumbo

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

Related Questions