2

I want to output this multi line html with variable into div from inside jquery but it never ouput the block of code inside div! I am using only variable inside it which is"itemName". could any one tell me why my code doesn't out to div ?

var siteContents2 ="<li> +
"       <iframe src='<iframe src='http://bsite.com/itemshow.php?"+itemName+"&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br>+
"        <div class="details">+
"        <div class="title">+
"          <a href="/+itemName/">+itemName</a>+
"        </div>+
"        </div>+
"    </li> ";   

document.getElementById("myDiv").innerHTML += siteContents2;


</script>   
</head>

<body>

    <div id="myDiv"></div>
user1788736
  • 2,727
  • 20
  • 66
  • 110

4 Answers4

3

Since ES6, you can store multi-line variables as template literals by surrounding them with backticks (`s):

var siteContents2 =`<li> 
       <iframe src='<iframe src='http://bsite.com/itemshow.php?`+itemName+`&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br>
       <div class='details'>
       <div class='title'>
       <a href='`+itemName+`'>`+itemName+`</a>
    </div>
    </div>
</li> `; 
document.getElementById("myDiv").innerHTML += siteContents2;

More on template literals here.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
Tyler Silva
  • 411
  • 6
  • 14
2

Your quotes and concats are not correct. try this:

var siteContents2 ="<li>" + 
 "       <iframe src='<iframe src='http://bsite.com/itemshow.php?"+itemName+"&title=ok&bgcolor=white' height=200 width=200 style='border: none;></iframe><br> "+
"        <div class='details'>"+
"        <div class='title'>"+
"          <a href='/"+itemName+"/'>"+itemName+"</a>"+
"        </div>"+
"        </div>"+
"    </li> ";  
krampstudio
  • 3,519
  • 2
  • 43
  • 63
  • Thanks . It worked well .could you tell me how i append these blocks of
  • so they get arranged in rows of 5 . Now they are printed one below each other! – user1788736 May 10 '13 at 09:33
  • i am trying to append the sitecontents2 to a div in body of page since am calling this script multiple time and want to arrange these outputs in rows of 5. Now each time i call the script it puts each iframe below each other. My goal is to have 5 iframe in a row! – user1788736 May 10 '13 at 09:51
  • so add your content in a sublist and add a new item to the parent list once the sublist size contains 5 items – krampstudio May 10 '13 at 10:05