2

I want to get the width of an element including border, margin and padding.

I know using $(input).width() or $(input).css('width') give the width of the element.

But here is the requirement.

Suppose I created a text box like follows:

<input type='text' id='txtBox' name='txtBox' style='width:100%' />

or

<input type='text' id='txtBox' name='txtBox' style='width:auto' />

If I get the width using either $input.css('width') or $input.width(). I am not able to get the width in pixels. I am using jquery1.3.2.

4
  • @tster - I am testing using Firefox. But I want to work this for all browsers. Commented Nov 18, 2010 at 7:01
  • stackoverflow.com/questions/1997419/…? Commented Nov 18, 2010 at 7:02
  • This should work, when do you try to acess the width() ? Commented Nov 18, 2010 at 7:02
  • @Dr.Molle - I am having a jquery ui sortable list. I am setting the width of all the list items as 'auto'. When I am sorting I need the width of the list element. Commented Nov 18, 2010 at 7:04

1 Answer 1

6

That should work just fine.

You are maybe missing to wrap your code into jQuery(document).ready().

See. http://www.jsfiddle.net/Cs4bu/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Thats working great there. But I don't know why is it not working in my application. I have the jQuery(document).ready().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.