ka_lin
ka_lin

Reputation: 9432

JQUERY Get value from <span> on click

The problem is not that difficult, I just can't get my head around it (noob at Jquery). The problem all comes down to when clicking a <span> getting it`s text and print it;

$('span').click(function(){
var t= ???;
    alert(t);
});

How can i get it's text??? NOTE: Each span does not have an id or class, any span clicked must output a message. Each span is generated dinamic via PHP and I need it's value.

Upvotes: 5

Views: 11315

Answers (4)

Bahaa Hany
Bahaa Hany

Reputation: 754

To get the span id without click

id = $("span.ClassName").attr('id');

Upvotes: 0

daveoncode
daveoncode

Reputation: 19588

use .text() (http://docs.jquery.com/Text())

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

Upvotes: 3

Brock Adams
Brock Adams

Reputation: 93493

$('span').click(function(){
    var t = $(this).text();
    alert(t);
});


See a demo at jsFiddle.

Upvotes: 10

jensgram
jensgram

Reputation: 31508

$('span').click(function(){
    var t= $(this).text();
    alert(t);
});

See .text().

Upvotes: 4

Related Questions