-3

http://i.imgur.com/p9ZkeIk.png

I create a search engine and I have a problem.

<div class="singleresult" onclick="benger()">
<img class="profilepic" src="http://graph.facebook.com/'+this.id+'/picture"  alt="Smiley face" height="50" width="50">
<p class="name">'+this.name+'</p> 
<p class="tapto" algin="right">TAP TO UNLOCK</p> 
</div>

how to get the name value by clicking on the div? They have all the same classes

Cattla
  • 3,570
  • 1
  • 13
  • 29
Danny
  • 11
  • 1
  • 4

3 Answers3

1

Use $(this) Selector.

<div class="test">test1</div>
<div class="test">test2</div>
<div class="test">test3</div>
<div class="test">test4</div>
<div class="test">test5</div>

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

try this fiddle : http://jsfiddle.net/rfeuhyq0/

vinrav
  • 371
  • 3
  • 12
  • its working but i need get value from class name which is in div class singleresult – Danny Nov 30 '14 at 14:00
  • Your fiddle is not working. So we cant understand the real problem.
    Smiley face

    '+this.name+'

    TAP TO UNLOCK

    from this which value you need
    – vinrav Nov 30 '14 at 14:05
0

This would retrieve specific text regarding which .singleresult DIV is clicked:

$(document).on("click",".singleresult", function(){
    var name = $(this).find('.name').text();
    alert(name);
});

That's said, sounds like you want to retrieve specific data, element text shouldn't be used for that but instead any custom data-* attribute.

A. Wolff
  • 72,298
  • 9
  • 84
  • 139
-1

Try the following:

$(document).ready(function(){
$(document).on("click",".singleresult", function(){
alert($(".name").text());
});
jGupta
  • 2,175
  • 4
  • 21
  • 48