12

Using jQuery to get the width of each item in a list

 2 years ago
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.
neoserver,ios ssh client

Using jQuery to get the width of each item in a list

advertisements

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?

I'm writing a jQuery plugin and something I need to be able to do is determine the width of an element that the user specifies. The problem is that .width() or .css('width') will always report exact pixels, even if the developer has assigned it e.g.

Suppose I have a set of arbitrary elements, can I use jQuery to set the height of each item to 100px?

So suppose I have a JavaScript array containing a bunch of elements, but these elements don't have anything in common (besides being members of the array). Is there any way to use jQuery to modify every element in this array at once? I'm currently ma

How to get the name of each item in a list using lapply ()?

Imagine that I have the following list > test <- list("a" = 1, "b" = 2) Each element of the list has a name : > names(test) Now, I want to extract that name using lapply() because I want to use it in a new function which will

Using jQuery to get the value of the multiple check box and output as a string separated by commas.

I have many checkboxs as below, <li><input type="checkbox" name="areaofinterest" value="home_coo" id="home_coo" class="Checkbox" > Cooking</li> <li><input type="checkbox&quo

Jquery gets the width of an item on the click

I am using the following jquery code to get the width of an SVG element when clicked on it and write it to the console. I get nothing on the console but no error as well. I am thinking this is because I am not passing the id correctly to the jquery s

How do I add items to a drop-down list, then get the text of each item

I have a dropdown of 10 items. How can I write a code that would store all these items in some sort of list so that i can compare each of the items to each other? The layout of the page is: <select id="blahBlah"> <option value = "0

print () that prints the contents of each item in your list

Basically i'm trying to write a print statement that will allow me to print the elements per line as a println as the output when i run the driver.java. And for the life of me i cannot figure out how to do it. Any help will be appreciated. here is th

Python: Count the instances of each item from a list of lists

I'm a newbie to coding. I need to count the number of occurrences of each item in a list of lists. Here is an example of the list of list I deal with: GC = [[5,4,3,2,1],[9,8,7,6,5,4,3,2],[4,3,2,1],[10,9,8,7,6,5,4]] and print the results in two column

Get the index of duplicate items in a list in c #

I am looking for a way to get the index of all elements in a list from a keyword search within the list. So for example my list has: Hello World Programming Rocks Hello Hello World I love C# Hello Now from this list of strings, i want to get all the

Get the number of duplicate items in a list

I am trying to get the count for the most common duplicate item in a list. So far, I have: List<string> brandList = new List<string>(); which contains 5 different soft drink brands, and the count is 100. I need to find out which brand has the

jQuery: get the width of each element and summarize them

How can I get each element's width and sum them up? For instance, here is the HTML: <div id="container"> <div class="block-item" style="width:10px"></div> <div class="block-item" style="wid

Using jquery to get the totals per line and the grand total table

So the short version of this is: Can I traverse only the elements within the matched element of the selectors before the each()? Or is there a simpler way of getting what I want without an each() loop? I thought this would be much easier, which makes

Use jquery to get the descendants of an element that are not children of a container with some CSS class

I have a situation in which i need to select all descendants of a certain element, but exclude those that are children of a container that equals the CSS class of the container i run my selector on. Pretty complicated description. <div class="cont

Use jquery to increase the width of the container by 10 rem

How can I increase the width of an element by 10rem using jquery. Issue is Jquery always return width/height in pixels and we want to update the height/width in some other units. Example on: JS Bin var $c = $('#container'); console.log($c.width()); v

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK