Randy Geily
Randy Geily

Reputation: 139

Jquery click on anchor and get value

I have these anchors generated by php script:

<a href="#" class="remFile"> Leaf </a>
<a href="#" class="remFile"> Flower </a>
<a href="#" class="remFile"> Branches </a>
<a href="#" class="remFile"> Seeds </a>

I need to click on e.g. Flower and alert "flower";

I used jquery:

alert( $(".remFile").html()); // output 1st item "Leaf" on any anchor clicked

I also tried:

alert( $(".remFile").text()); // output all values on any anchor clicked

If it is not possible, can you please suggest some other solutions? like using < li > ?

Upvotes: 0

Views: 80

Answers (6)

sharif2008
sharif2008

Reputation: 2798

use this -

$('.remFile').click(function(e) {
   e.preventDefault(); // or return false;
   alert($(this).html());
});

Live demo : click here

Upvotes: 1

WakeupMorning
WakeupMorning

Reputation: 1087

As I can understand you want to do this:

$(".remFile").click(function(){
    alert($(this).html());
});

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

try

$(".remFile").click(function(){ alert($(this).html())});

Upvotes: 1

just use this to refer to current anchor

alert($(this).text();

$('a').click(function(){
alert($(this).text());
});

Fiddle

Upvotes: 1

j08691
j08691

Reputation: 207901

$('a.remFile').click(function(){ alert($(this).text()) })

jsFiddle example

Upvotes: 3

Felix
Felix

Reputation: 38102

You can use $(this) to target current clicked anchor as well as text() to get the text of your anchor:

$('a.remFile').click(function(e) {
    e.preventDefault(); // Prevent default action of your anchor which will reload the page
    alert($(this).text());
});

Fiddle Demo

Upvotes: 2

Related Questions