0

Actually I want something like that:

var color = #000;

$(".div").find("li").css({"border-left": '5px solid [`I want to add this variable (color) here`]});

Please help me.

Thank you.

1
  • simple string concatenation Commented Apr 7, 2020 at 19:05

3 Answers 3

4

If you can use ES6 then a template literal is a good choice here. It uses backticks instead of other types of quotes.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

var color = "#000"; //change this to be a string as @Nikos mentioned

$(".div").find("li").css({"border-left": `5px solid  ${color}`});
Sign up to request clarification or add additional context in comments.

6 Comments

Looks like you beat me by 4 seconds, great minds :)
color should be a string though,
Good point. Ill update
It's not working.
Did you see the note about your var color = #000 missing quotes?
|
1

var color = "#00f";
$("#button").click(function(){
  $("div").css({"border": "5px solid " + color});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="button">Add Border</button>
<div>Hi, I am a div</div>

Comments

1
$(".div").find("li").css({"border-left": `5px solid ${color}`});

Comments