0

I want to create the width array of each children of selected element. I tried :

var width = $(element).children().map(function () { return this.style.width; }); 

But this is not working. When i alert(width[0]) then instead of showing width it shows objectHtmlElement. What i am doing wrong ?

0

4 Answers 4

3

Try .width() the style property only works if you set inline styles.

var widths = $(element).children().map(function () { return $(this).width(); }); 
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to get the basic array, you should call get method, map method returns a jQuery-wrapped array.

As the return value is a jQuery-wrapped array, it's very common to get() the returned object to work with a basic array.

var widths = $(element).children().map(function() {
     return this.style.width; 
}).get(); 

Comments

1
var obj = $('li');
var arr = $.makeArray(obj);

Result would be:

(typeof obj === 'object' && obj.jquery) === true;
jQuery.isArray(arr) === true;

Comments

0

use jquery width() and get() after map:

var widths = $(element).children().map(function() {
     return $(this).width(); 
}).get(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.