Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript/jquery - how to get the width of an element / css class that hasn't been added to dom yet

I'm trying to dynamically find the width of an item which will have a css class with a specific width, in order to dynamically position its background image (a sprite). However, the item has not yet been added to the DOM. Is there a way to read a class's width property before it's added to the DOM?

like image 873
mheavers Avatar asked Oct 07 '11 21:10

mheavers


1 Answers

I believe you cant. Instead add it to a test div,find the width and then remove the div.

   $selector.append("<div id='test'></div>");
   var widthVal= $selector.find("#test").width();
       $("#test").remove();

selector is the element selector you may want to append to. You can associate a class with the "test" div, to have it as "display:none"

like image 59
Raghav Avatar answered Nov 14 '22 23:11

Raghav