I have been trying to find a way to change part of the innerHTML of several elements with the class .studentYear, but I keep getting a JavaScript undefined error. Here's what I am trying.
JavaScript:
document.getElementsByClassName('studentYear').innerHTML.indexOf(" ").css('color', '#800000');
HTML:
<div class="accordionContainer">
<div class="studentYear">Freshman (A1 Schedule)</div>
<div class="accordionContent" style="display:none;">
<p class="studentTerm">Fall</p>
<p>Content</p>
</div>
</div>
I see now that it is a collection (array type). I need to access the innerHTML for each item in the collection and change the style attribute on only part of the innerHTML, in this case everything in parentheses "(A1 Schedule)". I still get the undefined error if following the suggestions made below.
innerHTML.index....on each element that way? What exactly is undefined? Do you have any more errors?document.getElementsByClassName(...)returns an array, and arrays don't have aninnerHTMLproperty.innerHTMLin array. You have toforeachon them. You also can't callcssmethod on a string. Simply get theinnerHTMLand makestring.replaceon it. I don't know what you except so I can't help you much...getElementsByClassNamereturns a HTMLCollection which is an array like object. You cannot callinnerHTMLon this object, you must iterate (jquery selectors abstract this step). Speaking of,cssis a jquery method, not a dom node method - you're looking forstylewhich is an object containing each style property. Not sure the point of theindexOf. Can you give an example of your expected behavior / output?