Wendy
Wendy

Reputation: 111

How do I pass a variable into html assigned to an element using jquery

I'm trying to do something like this:

$('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash"  style="background:#777 url(myVariable) no-repeat;">');

The html isn't showing up because it doesn't in code blocks here. Basically I'm trying to nest a variable like this: style equals background url(myVariable) in the html I'm assigning to the element dynamically.

Upvotes: 1

Views: 54

Answers (1)

Sergio
Sergio

Reputation: 28837

Since you started the string with single quotes you use ' again and + to concatenate like this:
url(' + myVariable + ').

So your code should be:

$('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash"   style="background:#777 url('+myVariable+') no-repeat;">');

More info about concatenation operator (in javascript) at MDN

Upvotes: 5

Related Questions