rdellara
rdellara

Reputation: 211

How do I include a variable in jquery .html

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?

Upvotes: 0

Views: 41

Answers (3)

Julio
Julio

Reputation: 2290

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.

Upvotes: 2

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

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

Upvotes: 1

Mike Corcoran
Mike Corcoran

Reputation: 14565

by building up the string by using the variable:

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

Upvotes: 1

Related Questions