Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  169] [ 4]  / answers: 1 / hits: 25493  / 11 Years ago, mon, october 28, 2013, 12:00:00

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


More From » jquery

 Answers
2

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());
});

[#74679] Sunday, October 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;