Using jQuery to get the width of each item in a list
source link: https://www.codesd.com/item/using-jquery-to-get-the-width-of-each-item-in-a-list.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Using jQuery to get the width of each item in a list
I am trying to get the width value of each list item in an element but it keeps returning the object and not the width. This should be really simple but for some reason I must be missing something.
Here is my code:
var listWidth = $('ul li').each(function(){
return $(this).width();
});
console.log(listWidth);
Use the map()
(docs) method instead to return the values to a jQuery object, then the get()
(docs) method to convert it to an Array.
var listWidth = $('ul li').map(function(){
return $(this).width();
}).get();
Or similar, you can use the jQuery.map()
(docs) method, which returns an Array.
var listWidth = $.map($('ul li'),function(val){
return $(val).width();
});
Related Articles
Is it possible to use jQuery to get the width of an element in percentage or in pixels, depending on what the developer specified with CSS?
Suppose I have a set of arbitrary elements, can I use jQuery to set the height of each item to 100px?
How to get the name of each item in a list using lapply ()?
Using jQuery to get the value of the multiple check box and output as a string separated by commas.
Jquery gets the width of an item on the click
How do I add items to a drop-down list, then get the text of each item
print () that prints the contents of each item in your list
Python: Count the instances of each item from a list of lists
Get the index of duplicate items in a list in c #
Get the number of duplicate items in a list
jQuery: get the width of each element and summarize them
Using jquery to get the totals per line and the grand total table
Use jquery to get the descendants of an element that are not children of a container with some CSS class
Use jquery to increase the width of the container by 10 rem
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK