Reputation: 2819
I have this HTML:
<span class="redColor">gold</span>
And when I have the text selected that is within the span tags, I need to be able to somehow extract the className from the span using javascript. I don't know anything about the class in advance, only that it will be in a span, and the text will be selected.
Upvotes: 0
Views: 345
Reputation: 10100
I assume you mean Text selected with the mouse or the keyboard? You can access that with
window.getSelection()
Then you can work your way up the DOM tree:
window.getSelection().anchorNode.parentNode.className
See https://developer.mozilla.org/en-US/docs/Web/API/Selection.anchorNode for documentation of the Selection class
Upvotes: 2
Reputation: 15501
Use className
property.
Example:
var x = document.getElementsByTagName('span');
document.write("Body CSS class: " + x.className);
Upvotes: 0