Ad

Jquery: Select Class Where $(this) Is

- 1 answer

I want to select a div using its class, but there are other divs with the same class and with the same buttons that trigger my function, so I only want to select the class where $(this) is in.

I tried .parent, .child, .contains, .find but I didn't figure it out...

$(".btt_class").click(function() {
  
  // I know this is wrong, but to give an idea of what I need
  $(".div_class").has(this).append("You cliked here");
  
});
<div class="div_class">
  <input class="btt_class" type="button" value="Click me" />
</div>

<div class="div_class">
  <input class="btt_class" type="button" value="Click me" />
</div>

Ad

Answer

If I understand your request correctly, your snippet does just what you described: selecting the div, the currently clicked button is in and appending some text to it:

Pleas check out https://jsfiddle.net/4qwy605p/

$(".btt_class").click(function() {

  $(".div_class").has(this).append("You cliked here");

});

<div class="div_class">
  <input class="btt_class" type="button" value="Click me" />
</div>

<div class="div_class">
  <input class="btt_class" type="button" value="Click me" />
</div>

It works as requested in the fiddle.

Is it possible you just forgot to load jQuery in your snippet?

Ad
source: stackoverflow.com
Ad