1

While clicking on a div (any 'resultsMainContainer' div) i should get the div id with class 'result' inside that particular div.

<div class="resultsMainContainer">
 <div id="48" class="sbsCard panel  result">
   abcd
 </div>
</div>

<div class="resultsMainContainer">
 <div id="49" class="someCard result">
  aabb
 </div>
</div>

<div class="resultsMainContainer">
 <div id="50" class="sbsCard panel3 result">
   nnaaa
 </div>
</div>

<div class="resultsMainContainer">
 <div id="33" class="sbsCard panel  result">
   abcddd
 </div>
</div>

I tried this:

$('body').on('click', '.resultsMainContainer', function() {
         alert($(this).next(".result").attr("id"));
});

But it's does not seems working. Is there any other idea/options ?
Or did i made any mistake :) ?

smilyface
  • 5,021
  • 8
  • 41
  • 57

3 Answers3

2

Use .children() or .find() instead of .next()

$('body').on('click', '.resultsMainContainer', function() {
     alert($(this).children(".result").attr("id"));
});

and

use .prop() instead of .attr()

alert($(this).children(".result").prop("id"));

Read .prop() vs .attr()

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

use jQuery.find(), change to:

alert($(this).find(".result").attr("id"));
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

Use find instead of next, as .result is inside "resultsMainContainer" not next to it.

$('body').on('click', '.resultsMainContainer', function() {
  alert($(this).find(".result").attr("id"));
});
Sharique
  • 4,199
  • 6
  • 36
  • 54