0

What's wrong with my concatenation below?

$(function(){

    var bg = "http://i.imgur.com/kqRNO6M.jpg";
    var html = '<div style="background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)),url(\"'+ bg +'\") no-repeat;">';
});

my demo is here http://jsfiddle.net/su3fpkex/

I escaped double quote with \ but couldn't seem working.

2
  • console.log(html) and check the quotes ;) Commented Feb 5, 2015 at 13:44
  • I'm confused...you set some variables then don't do anything with them? Commented Feb 5, 2015 at 13:44

4 Answers 4

2

try with this:

$(function(){

  var bg = "http://i.imgur.com/kqRNO6M.jpg";
  var html = '<div style=\'background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)),url("'+ bg +'") no-repeat;\'>';

});

and remember to append somewhere your html variable! :)

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

3 Comments

sure, you have to use your "html" variable in one way or another :D it's just to explain how to concatenate correctly these strings :)
this work but i don't understant the concatenation here, why escpae from the beginning?
if you escape the ' when specifying attributes you can use plain quotes inside the string. I prefer this way :)
1

You forgot to put the html content in the page:

$(function(){

    var bg = "http://i.imgur.com/kqRNO6M.jpg";
    var html = '<div style="background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)),url(\''+ bg +'\') no-repeat;">';
    $('body').append(html);
});

Comments

0

Try this

$(function(){

    var bg = "http://i.imgur.com/kqRNO6M.jpg";
    var html = '<div style="background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)),url(\''+ bg +'\') no-repeat;">';

    alert(html);
});

Comments

0

If you set background image you have to set width and height for div.

<div class='img'></div>

$(function(){
   var bg = "http://i.imgur.com/kqRNO6M.jpg";
   var html = '<div style="width:300px;height:300px;background-size:cover;background:linear-gradient(rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.35)),url(\''+ bg +'\') no-repeat;">';
    $('.img').html(html);

});

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.