Any ideas why this doesn't work?
<a href="javascript:alert($(this).text())">Alert This Text Here</a>
I am expecting it to alert "Alert This Text Here", but instead I get a jQuery error (G is undefined)
What am I missing?
I would assume that "this" has no association to the link you're clicking. It was never given association. this
doesn't derive its association from markup/syntax.
The following works:
<a href="#" onClick="alert($(this).text());">Some Text</a>
Ideally, you want to keep your scripting separate from your markup. You should do it like this:
$("a.someClass").click(function(event){
event.preventDefault();
alert($(this).text());
});
This example would require your anchor to have a class="someClass" to function.
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