Child element's width is undefined

285
November 26, 2016, at 7:29 PM

code:

$(".article-image").each(function(){
    var imgW = $(this).children("img").naturalWidth;
    console.log(imgW);
})

$(this).children("img”).length is 1, however imgW is undefined.

I've also tried:

$(this).children("img").scrollWidth;

And this was 0:

$(this).children("img").width();
Answer 1

I was trying to read the image width before it loaded. So this is what I did:

$(".article-image").each(function(){
    $(this).children("img").load(function(){
        var imgH = $(this).parent().children("img").height();
})
Answer 2

Use outerWidth

$(".article-image").each(function(){
   var imgW = $(this).children("img").outerWidth;
   console.log(imgW);
})
Answer 3

Try:

$(this).find("img").eq(0).css("width");

If you need width of specific element, you can give the element class name:

$(this).find(".className").css("width");
Rent Charter Buses Company
READ ALSO
how to call something specific from a rest api

how to call something specific from a rest api

I'm trying to call a specific stat from a rest API

365
jQuery with images sliding

jQuery with images sliding

I have these codes:

281
Bootstrap Touchspin Change event

Bootstrap Touchspin Change event

my code doesn't seem to work on change event when I use the touchspin buttons, but if I change something in the input field it works

1008
Backbone events issues

Backbone events issues

I got one button in my DOM, I added to it (in backbone) a click eventlets call it event1

295