JavaScript Class not working jQuery class click not working jquery,class selector not working jquery,

So I more than one dynamicly generated elements with the same class name that I am trying to check input for in jQuery. Instead of it letting me click on both, it is just letting me click the first element generated.

Ex: I click on item_1 and it returns the item_1 id, but when I click on item_2 it doesn't return anyting.

HTML

    <div id="item_1" class="resp"></div>
    <div id="item_2" class="resp"></div>

JS - Jquery

    $(".resp").on("click",() =>{
      var id = $(".resp").attr("id");
      console.log('attempting toggle' + id);
    });
Answer:1

Firstly, you have to use normal function instead of arrow function (to avoid missing the context). Secondly - use this keyword to refer to the actually clicked element.

$(".resp").on("click", function() {
  console.log('attempting toggle ' + this.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="item_1" class="resp">A</div>
<div id="item_2" class="resp">B</div>
Answer:2

This is because .attr('id') returns the value of the id attribute of the first matched element in the set.

Instead, use an old school function for the handler so the this value is equal to the clicked div, then get its id:

$(".resp").on("click", function() {
  var id = $(this).attr('id');
  console.log('attempting toggle ' + id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="item_1" class="resp">First</div>
<div id="item_2" class="resp">Second</div>
Answer:3

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...

I have created custom tooltip for my chart in chartjs. But the issue comes when I hover on the chart, the tooltip starts flickering. Anyone knows how can I prevent this? Thanks! if (!tooltip) { ...

I have created custom tooltip for my chart in chartjs. But the issue comes when I hover on the chart, the tooltip starts flickering. Anyone knows how can I prevent this? Thanks! if (!tooltip) { ...

Trying to authenticate users on my ionic application through an external service and I need to use cordovas In app browser! The code works perfectly on android however on iOS the "loadstop" event ...

Trying to authenticate users on my ionic application through an external service and I need to use cordovas In app browser! The code works perfectly on android however on iOS the "loadstop" event ...

  1. cordova browser event

There are two conditions: The div (and the divs inside) must have a variable height. The divs inside must add a border on hover over them. There is a problem: The border of the hover adds height to ...

There are two conditions: The div (and the divs inside) must have a variable height. The divs inside must add a border on hover over them. There is a problem: The border of the hover adds height to ...