2

here's my simple code:

var msgbox = $("<div style='width:320px;height:200px;'></div>");
alert(msgbox.width());

the alert gives me zero - what's wrong? why isn't it 320?

thx

5
  • Can you try adding a &nbsp; to the div? Commented Aug 7, 2010 at 12:30
  • 1
    Or try inserting the msgbox into the DOM before measuring its .width(). Commented Aug 7, 2010 at 12:31
  • 1
    isn't the $("") should be a selector rather then HTML? Commented Aug 7, 2010 at 12:32
  • @Ankit Jain: It can also be used to create HTML, see api.jquery.com/jQuery/#jQuery2 Commented Aug 7, 2010 at 12:33
  • then how does jQ differentiates b/w a selector and HTML? Is that first character as < is sufficient? Commented Aug 7, 2010 at 13:24

2 Answers 2

5

You should insert your div in the document first. Or use .css("width") instead.

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

Comments

4

You need to first append the element to the DOM before trying to measure its size:

$('body').append(msgbox);
alert(msgbox.width());

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.