Reputation: 3
the 'bg' is recognised as a variable .. its supposed to be text .. i also tried the double quotes but the url() only accepts single quotes url
var ahref = '<a href="#" style="background-image: url('bg');"></a>';
var newhref = ahref.replace('bg', newThumbnailUrl)
Upvotes: 0
Views: 34
Reputation: 20944
JavaScript string interpolation works differently.
You can use Template literals, using the backtick ` `
.
var bg = 'your-image.jpg';
var ahref = `<a href="#" style="background-image: url('${bg}');"></a>`;
console.log(ahref);
Or you can concatenate strings with the addition operator +
.
var bg = 'your-image.jpg';
var ahref = '<a href="#" style="background-image: url(\'' + bg + '\');"></a>';
console.log(ahref);
The url()
property does not require quotes, only in certain conditions.
A url, which is a relative or absolute address, or pointer, to the web resource to be included, or a data uri, optionally in single or double quotes. Quotes are required if the URL includes parentheses, whitespace, or quotes, unless these characters are escaped, or if the address includes control characters above 0x7e. source
Upvotes: 1