javascript - Why add "+" and double quotes to access the object's src? -


when mouse on preview class image, #image div display image instead of original plain text. javascript function works well. have question using document.getelementbyid('image').style.backgroundimage="url('"+previewpic.src+"')"; parameter’s url.

why have add " , + before previewpic.src? console.log(previewpic.src) display image’s url, tried use "url('previewpic.src')" access url, doesn’t work. what’s reason?

demo at: http://codepen.io/betty-liu/pen/goxlpb

<body>     <div id = "image">         hover on image below display here.     </div>     <img class = "preview" alt = "styling bandana" src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/389177/bacon.jpg" onmouseover = "update(this)" > </body> 

javascript function:

function update(previewpic){     document.getelementbyid('image').style.backgroundimage="url('"+previewpic.src+"')";     document.getelementbyid('image').innerhtml = previewpic.alt;  } 

erm, that's string concatenation , used join strings including variables , properties.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -