I have an empty h2 element with width=0. Afterwards I put some content in the h2 with JavaScript. How can I get the new width of the h2 element?
Before: <h2></h2>, then I do $('h2').html('hello');, then i have: <h2>hello</h2>.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
.width()is how you get the live width of an element.Your bigger problem is that setting a block level element’s width statically, does not allow it to expand to fit content. If you set the css of that h2 to
overflow:hiddenyou will probably see what I mean.Header tags by default fill the available width of the parent even when empty.