0

Is there anyway to get the width of a element using the class name of a child element?

 <td width='100%' nowrap='1' style='padding-left:2px; margin:2px;'>
     <font class='DialogHeaderTitleFont'>RudimentalDrumming.com Jackets</font>
 </td>

In the above code, I would like to get the width of the element by using the class name of the element, ".DialogHeaderTitleFont", is that possible?

Thanks.

Morgan Kenyon
  • 3,072
  • 1
  • 26
  • 38

2 Answers2

2

You can use .width function to get the width of an element.

$('.DialogHeaderTitleFont').parent().width();

jQuery offers different variation of width function such as innerWidth, width and outerWidth.. For more details check this Find out what the width of an image is using jQuery

Edited: Missed that you want the width of the parent element.Thanks @Kevin

Community
  • 1
  • 1
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • 1
    *"using the class name of a **child** element"* may need to add `.closest` or `.parent` to that. font tags also don't generally have a width. – Kevin B Jan 15 '13 at 22:37
  • @Vega. I'm with KevinB. You should use `.closest` to accommodate for future changes, especially so given the OPs use of legacy elements. Won't be long before that gets wrapped with `` :) – Paul Fleming Jan 15 '13 at 22:43
0

yep just use .parent();

$(".DialogHeaderTitleFont").parent().css("width");

keep in mind if you repeat this class anywhere in your document variable may change, so I suggest you use some kind of unique identifier like an id and not a class

Paradise
  • 470
  • 2
  • 11
  • +1 for good answer (though I think `.closest` would be better). -1 for "suggest you use some kind of unique identifier like an id". Result - 0 – Paul Fleming Jan 15 '13 at 22:45
  • using classes to get specific values is not the best practise I can assure you that, unless you actually write that class for jquery purposes http://stackoverflow.com/questions/1230636/css-optimization-element-id-vs-class?#answer-1230650 – Paradise Jan 15 '13 at 22:52
  • You're making an assumption that the class won't be used in multiple places for styling purposes. – Paul Fleming Jan 16 '13 at 13:39