0

I have 4 div which has this is li#click, and i want to get the class of this id. I am trying to get class from de current div, but it come undefined. i use "this" to get class on the clicked div only, because is there more div with the same function

i use the jquery function .attr('class') to get the class using the "this" keyword. on the first time it work fine, but when i restart de pc it does not work anymore

$(document).ready(function(){
    // code para about device drop panel
    $( "li#click" ).click( () => {
        var classi = $(this).attr('class')
        alert(classi)
        let q = `#${classi}`

        $(q).toggleClass( "dados2" )

    });

});

i trying to get a class a name but it come undefined

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Coyas
  • 73
  • 1
  • 10

1 Answers1

1

You should use a regular function, not an arrow function. Arrow functions don't make their own this variable. Using an arrow function, your code would look like this:

$(document).ready(function(){
    // code para about device drop panel
    $( "li#click" ).click(function() {
        var classi = $(this).attr('class')
        alert(classi)
        let q = `#${classi}`

        $(q).toggleClass( "dados2" )

    });

});
Kavelin
  • 397
  • 1
  • 11