JQUERY Get value from <span> on click

10,200

Solution 1

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


See a demo at jsFiddle.

Solution 2

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

See .text().

Solution 3

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

$('span').click(function(){
        alert($(this).text());
    });
Share:
10,200
ka_lin
Author by

ka_lin

Well I am a curios person regarding anything programmable but who enjoys mostly PHP and algorithms :-)

Updated on June 09, 2022

Comments

  • ka_lin
    ka_lin almost 2 years

    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.

  • Brock Adams
    Brock Adams almost 13 years
    You're welcome. Note that the other two had the same answer just a few seconds after mine.