how to get text from an link with onclick ?
my code :
<a href='#' onclick='clickfunc()'>link</a>
function clickfunc() {
var t = text();
alert(t);
}
text = link
try this
<a href='#' onclick='clickfunc(this)'>link</a>
function clickfunc(obj) {
var t = $(obj).text();
alert(t);
}
well, it is always better and recommended to avoid inline javascript(onclick()
).. rather you can use
$('a').click(function(){
alert($(this).text());
});
or to be more specific...give an id to <a>
and use id selector
<a href='#' id='someId'>link</a>
$('#someId').click(function(){
alert($(this).text());
});
<a href='#' onclick='clickfunc(this)'>link</a>
clickfunc = function(link) {
var t = link.innerText || link.textContent;
alert(t);
}
JSFiddle Demo
try this with vanilla javascript
<a href='#' onclick='clickfunc(this)'>link</a>
function clickfunc(this) {
var t = this.innerText;
alert(t);
}
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