4

I want to get element width using jQuery. The problem is when I change html inside this element. I want to get its width and width() returns its parent width instead, it looks like html() doesn't update element width at all.

But... It works perfectly when no DOCTYPE is specified. I'm using IE in WebBrowser control.

css:

 p {
    margin: 0;
    padding: 0;
   }

 .name {
    font-weight: bold;
    }

 .description {
    color: #444;
    width: 190px;
    white-space: nowrap;
    overflow: hidden;
    }

 #container {

    }

html looks like this:

<div id="container">
<div id="friend0"><p class="name">Name</p><div class="description"><p>Description</p></div>
</div>

js:

var x1 = GetFriend(id).children('.description').width();
var x2 = GetFriend(id).children('.description').children('p').width();

(some jQuery animation code) and after that:

GetFriend(id).children('.description').children('p').html('some now text here...');
var x3 = GetFriend(id).children('.description').children('p').width();

and x3 is the same as .description with value of 190 but when I don't declare DOCTYPE at the beginning of html it returns correctly updated value.

I need to have DOCTYPE declared because some animation code causes flickering of scrollbars.

Anyone know how to do it correctly?

7
  • 7
    You already are doing it correctly. Block elements like <p> will use the width of their parent; that's the way the standard box model works. It changes when you drop the DOCTYPE because you're toggling the browser in and out of quirks mode. Commented Dec 27, 2010 at 16:44
  • 2
    @Pointy is correct. To prove it, turn your <p> in to a <span> and you'll notice width works as expected. Commented Dec 27, 2010 at 16:49
  • It works perfectly! Thank you guys for immediate help :) Commented Dec 27, 2010 at 16:52
  • 1
    Can you mark this an answered, Daniel? Commented Jan 18, 2011 at 16:13
  • @Pointy: Could you please post your comment as an answer so we can upvote it and get this question off the Unanswered list? Thanks. Commented Jan 20, 2011 at 0:27

1 Answer 1

1

You already are doing it correctly. Block elements like <p> will use the width of their parent; that's the way the standard box model works. It changes when you drop the DOCTYPE because you're toggling the browser in and out of quirks mode.

(Comment posted as answer ...)

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

Comments

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.