0

Trying to include a variable in my jquery:

var width = $(this).width();
$("#dynamic").html(".dropdown-bottom:before{left:'width'px;}");

How do I indicate that width is a variable in the .html?

3 Answers 3

2

Concatenate the strings together:

var width = $(this).width();
$("#dynamic").html(".dropdown-bottom:before{left:" + width + "px;}");

To subtract from your width, do so before the concatenation:

var width = $(this).width() - 20;

Since the documentation indicates that width() returns an integer, a simple subtraction should work here. This is of course presuming that this is actually referencing the correct thing.

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

2 Comments

@rdellara, according to the docs for width, it returns an integer representing the width in pixels. Prior to the concatenation, you'd need to subtract 20 from the width. See my edit.
Thanks, I figured this out another way $("#dynamic").html(".dropdown-nav:before{left:" + (width - 20) + "px;}");
1

by building up the string by using the variable:

$("#dynamic").html(".dropdown-bottom:before{left:" + width + "px;}");

Comments

1
$("#dynamic").html(".dropdown-bottom:before{left:" + width + "px;}");

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.