0

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?

1

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

0

You may mean this:

oCell = document.createElement("TD");
oSub = document.createElement("sub");
oSub.innerHTML = data;
oCell.appendChild(oSub);
oRow.appendChild(oCell);

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.