Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of element on which the event fired [duplicate]

Possible Duplicate:
Select inner text (jQuery)

I have a span:

<span class="test"> target_string </span>

And the event, that is fired on click on any element of the page.

$('body').click(function(event){
  if ($(event.target).attr('class') == 'test'){
    alert(???);
  }
}

How can I obtain target_string value?

like image 777
mol Avatar asked May 02 '12 20:05

mol


3 Answers

Use $(event.target).text() to get the text.

like image 185
Rocket Hazmat Avatar answered Nov 11 '22 04:11

Rocket Hazmat


Possibly more efficient to delegate from the body:

$('body').on('click', '.test', function(event){
    alert($(this).text())
});
like image 3
Simon Smith Avatar answered Nov 11 '22 04:11

Simon Smith


Try below,

$('body').click(function(event){
  var $targ = $(event.target);
  if ($targ.hasClass('test')){
    alert($targ.text());
  }
}
like image 1
Selvakumar Arumugam Avatar answered Nov 11 '22 04:11

Selvakumar Arumugam