Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery .style is undefined

I have a lot of tables in my page. Some tables have Width property defined inline -

style="width:100%; ..."

I need to get all such tables. I tried

$('.Forum_Container table').each(function () {
    var $this = $(this);
    if ($this.style != undefined) {
        if ($this.style.width == '100%') {
            ...
        }
    } else {
        ...
    }
});

but $this.style is always undefined. What can cause this problem?

like image 716
Sanya530 Avatar asked Jan 17 '13 12:01

Sanya530


1 Answers

When you do $(this) you're creating a jQuery object that contains a reference to that DOM element. The style property is a property of the DOM element itself, not the jQuery object, so trying to access it won't work (because it is undefined).

Either use this (the actual DOM node) directly - i.e. this.style.width - or use the functions provided by jQuery to access the information you need.

like image 168
Anthony Grist Avatar answered Sep 21 '22 02:09

Anthony Grist