I can't find any good documentation on this suprisingly so I'm posting it here.
What's the raw javascript equivilent to this:
$(elem).click(function(){
alert(this.text());
});
All I can find is this <elem onlick="func()" /> which is not what I want, I want to be able to do it just with javascript not within the context of an element.
Assuming elem is an element node (if not, you'll need to work your selector into use of getElementById or whatever):
elem.onclick= function() {
var text= 'textContent' in elem? elem.textContent : elem.innerText;
alert(text);
};
textContent is the standards-compliant way of getting text from an element; innerText is the IE way. (There are a few differences, but hopefully nothing that will affect you.) If you need to support old/obscure browsers that have neither you would have to do an tedious tree-walk to pick up all text (this is what jQuery's text() does.)
(I'm assuming that's what you want from this.text(). That wouldn't actually work — presumably you meant $(this).text().)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With