Old 23-08-2007, 07:55   #1 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
JQuery question...

Afternoon all,

With JQuery is it possible to find the length of something (a list item in this case) and then add a CSS rule (some padding) to another item using that length value?

Much appreciated.
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 23-08-2007, 09:15   #2 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Do you mean the character length or the px length?
  Reply With Quote
Old 23-08-2007, 10:14   #3 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
Teh posters!

The pixel length.
__________________
That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
  Reply With Quote
Old 23-08-2007, 12:07   #4 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
As the list items are block-level, they expand to fill the available space by default. So, to get the px width of the content, rather than the list item itself, you'll either need to make them inline, float and clear them or wrap the textual content in an inline element - e.g. span. This'll mean the list item will wrap neatly around the content.



Code:
$(document).ready(init); function init() { var el = $('.thisitem')[0]; var elCharL = el.firstChild.length; var elPXw = $(el).width(); var elTxt = $(el).text(); alert('element: ' + el + '\ncharacter length: ' + elCharL + '\npixel width: ' + elPXw + '\ntext: ' + elTxt); } … <ul> <li>item one</li> <li class="thisitem">item two</li> <li>item three</li> <li>item four</li> </ul>

If you're wrapping the content in a span, then obviously you'll need to target the span element in the el var instead…

Code:
var el = $('.thisitem span')[0]; … <ul> <li><span>item one</span></li> <li class="thisitem"><span>item two</span></li> <li><span>item three</span></li> <li><span>item four</span></li> </ul>
  Reply With Quote
Old 28-08-2007, 16:54   #5 (permalink)
3n1gm4
HEYOOOH!
 
Join Date: Mar 2006
Location: Denton, Tx
Posts: 52
regular javascript can, look into offsetHeight and offsetWidth
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8