greenbandit
greenbandit

Reputation: 2275

get all selectors inside a div

I'm trying to get all the selectors and get the height on that selector:

    var myDiv = $('#main-div').find('*').each(function(e){
        return this.id;
    });

    console.log(myDiv);

What I need is get the height and width of every element inside #main-div, and show into console like:

[selector, width , height]... each

Any ideas?

Upvotes: 0

Views: 104

Answers (3)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You can use .children() to access all the elements inside the div and use .height() and .width() on them. Something like below should work (untested),

$.each ($('#main-div').children(), function (index) {
   console.log ('[' + this.tagName + ',' + $(this).height() + ',' + $(this).width() + ']');
});

DEMO here to show that it only access the immediate childrens and not goes inside the child element(s).

Upvotes: 1

Wes Crow
Wes Crow

Reputation: 2967

Try:

var myDiv = $('#main-div').find('*').each(function(e){
    console.log( "id: " + $( this ).attr('id') + ", height:" + $( this ).height(  ) + ", width: " + $( this ).width(  ) 
});

Upvotes: 0

Ricardo Bin
Ricardo Bin

Reputation: 887

jQuery have width and height methods to do this

Upvotes: 0

Related Questions