JQuery Div attr (width = $ ('# immagineCont'). Attr ('clientWidth'); = undefined

im no expert javascript coder. I used like 5 hours trying a lot of different things. and i noticed some things i can't understand im trying to make this work

width = $('#immagineCont').attr('clientWidth'); 

but returns undefined i tried doing

 width = $('#immagineCont').attr('id'); 

the witch returned #immagineCont, I think it’s right.

when i use

  $('#immagineCont') 

I get this in firebug 1) http://bildr.no/view/1325119 2) http://bildr.no/view/1325120

so what i am trying to do is get a damn client. The width of the witch seems impossible. I even tried children ("0") and children ("# 0") I will kill my brain soon ^^

+4
source share
2 answers

attr will only work if there was an attribute with that name.

You can just use

 width = $('#immagineCont').get(0).clientWidth; 

but you will likely need

 width = $('#immagineCont').width(); 

which is more reliable when you want to do something that works the same in browsers (but does not include indents and borders).

You may also be interested in outerWidth .

A note on what happens and the need to get(0) : a jQuery collection (for example, $('#immagineCont') ) wraps one or more standard DOM objects that you can use with get(i) or [i] . If you want to access the native properties of a DOM object when jQuery does not offer a proxy function, you need to get this native object first.

+4
source

To do this, use jQuery width() :

 var width = $('#immagineCont').width(); 

There is also no attribute set for an element named clientWidth. This is a javascript property, so it can be restored by dereferencing a jQuery object:

 var width = $('#immagineCont')[0].clientWidth; 
+1
source

Source: https://habr.com/ru/post/1447936/


All Articles