3

I use the following code.

<div class="tt-suggestion">
    <p style="white-space: normal;">
        <span id="1">Rajesh<span style="float:right;">Bangalore</span>
        </span>
    </p>
</div>

When i click on , i need to have inner id.Below is my code to get the span id

$(document).on('click', '.tt-suggestion', function(){
var id=$(this).find("span").id;
});

How many values get from the database that many number of times that repeats. When i click on that , if i get the id, I have to send that id as a parameter to other php using ajax.But i am not able to get the id. Can anyone please help on this,Thanks.

Ram
  • 117
  • 1
  • 2
  • 13

3 Answers3

5

You'll have to use attr (doc link), since jQuery objects do not have an id property:

$(document).on('click', '.tt-suggestion', function(){
    var id = $('span', this).attr('id');
});

I've also removed the call to find to replace it with a more elegant form, although that's of course my opinion and not completely necessary.

tckmn
  • 57,719
  • 27
  • 114
  • 156
0

simply you can call the function attr to get any element attribute value

$(document).on('click', '.tt-suggestion', function(){
var id=$(this).find("span").attr('id');
    alert(id);
});

you can test you code here https://jsfiddle.net/qumop5pk/

0

Really simple call this function and you will get it... try ;-)

$('span').click(function (){

var Id=$(this).attr('id');

alert(Id);

});
giuseppe
  • 105
  • 9