1

I want to get this class to show up with the var newCaption. Below the Javascript I have linked the CSS Class.

window.onload = function() {

var caption = document.getElementsByTagName("caption");
var oldCaption = caption[0].innerHTML;
var newCaption = oldCaption + "CAPTION";

caption[0].removeChild(caption[0].firstChild);
caption[0].appendChild(document.createTextNode(newCaption));

Here's my CSS Class:

.hoverNode{
   background-color:#FFFFCC;
   border: solid 1px black;
   width:100px;
   height:100px;
   position:relative;
   float:top;
   font-size:10pt;
   margin:5px;
   padding:3px;
   color:black;
}
1
  • Show you html. You should have a class="hoverNode" Commented Feb 5, 2016 at 20:35

3 Answers 3

3

Add the class to the caption element.

caption.classList.add('hoverNode');
Sign up to request clarification or add additional context in comments.

Comments

0

In Firefox, the getElementByTagName() seems to be ignoring the caption. Perhaps because it is only permitted with a table as a parent. It's showing an error that caption[0] is undefined.

Try changing your caption to a p and it seems to work.

window.onload = function() {

var caption = document.getElementsByTagName("p");
var oldCaption = caption[0].innerHTML;
var newCaption = oldCaption + "CAPTION";

caption[0].removeChild(caption[0].firstChild);
caption[0].appendChild(document.createTextNode(newCaption));
}

Of course you'll need a <p> element to add the text to.

<caption>Some text</caption>
<p>Some Paragraph</p>

1 Comment

Or, if you surround your <caption> element with a <table> element it will also work.
0

This works!

var caption = document.getElementsByTagName("caption");
var oldCaption = caption[0].innerHTML;
var newCaption = "CAPTION";

var span = document.createElement('span');
var text = document.createTextNode(newCaption);
span.appendChild(text);
span.className = "hoverNode";
caption[0].appendChild(span);

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.