How can i get the content of a specific div sharing the same class with other div ? i get an empty string with 'this'

Advertisements

What i tried :

const adressLine = $('.adressLine');
    adressLine.on('click',()=>{
        console.log('button cliked');
        var text = adressLine.text();
        console.log(text);
    });

console result, i get all of the strings, not what i want :

Result

so i find a solution in other discussions:

const adressLine = $('.adressLine');
    adressLine.on('click',()=>{
        console.log('button cliked');
        var text = $(this).text();
        console.log(text);
    });

it returned me an empty string , why ?

Second result

>Solution :

This happens because you are using an arrow function.

Try with

const adressLine = $('.adressLine');
adressLine.on('click',function(){
    console.log('button cliked');
    var text = $(this).text();
    console.log(text);
});

Leave a ReplyCancel reply