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);
}
link.textContent || link.innerText for full browser support. Or stick to jQuery’s .text()You can do this:
HTML
<a href='#' onclick='clickfunc(this)'>link</a>
JS
function clickfunc(obj) {
var t = $(obj).text();
alert(t);
}
Demo: Fiddle