javascriptjqueryeachoffsetwidth

Trying to get width of element inside .each()


I am trying to get the width of an element whilst inside a .each() function however I am running into issues with it simply not working.

jQuery(document).ready(function($) {
    $('.tooltip').each(function() {
        var self = $(this);

        var width = self.find("#testing").offsetWidth;
        alert(width); //Returns undefined

        $(this).find(".tooltip-icon").hover(function(){
            self.find(".tooltip-text").show();
        }, function () {
            self.find(".tooltip-text").hide();
        });
    });
});

Solution

  • you are using jquery and javascript in the same line

    do this

    var width = self.find("#testing").width(); // jquery. you should change the name of the `self` variable to another one