var gunboxwidth = ($('h2 span').widest().width() + "px" + 30 + "px")
.widest() grabs the wides span inside of an h2, the problem i’m having is that it works fine when i DO NOT have
+ 30 + 'px'
why is that? is my syntax wrong?
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.
Try:
This adds 30 to the numerical value returned from the
widthfunction, then converts it to a string and adds the ‘px’ suffix.For example, if the width of the span is ’20’, the JQuery
widthfunction will return the number 20 – not a string like ’20px’. So the above code adds 30 to that and results in the string ’50px’. What you have would result in a string value of:…since when you add strings together, the second is just appended to the first.
You’re probably better off using:
…to keep it numeric, and then using that to e.g. set the width of another item using JQuery’s
widthfunction again, like: