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>

4 Answers 4

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.

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

Comments

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> ";  

3 Comments

Thanks . It worked well .could you tell me how i append these blocks of <li></li> so they get arranged in rows of 5 . Now they are printed one below each other!
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!
so add your content in a sublist and add a new item to the parent list once the sublist size contains 5 items
1

You're missing some quotes from the end of some of the lines and you can use either single quotes (') or an escaped double quote (\") within the html.

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;

Comments

1

Your code had erroneous quotes throughout it so it was technically an invalid string and most likely being rejected. In addition, you can do multiline strings by ending each line with a \ so you don't have to worry about so many quotes.

Your code, rewritten and working, could look like this:

var itemName = "blahblah";
var siteContents2 = "<li>\
       <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;

Here's a jsFiddle of the code: http://jsfiddle.net/vkaC8/

1 Comment

Ah you're right there was an error, sorry about that. The iframe was output but it's mark up was invalid because the code in you're initial question didn't have the trailing ' on this part: style='border: none;' and when i copied/edited the code i didn't realize that was missing. :/ I added the apostrophe and added a jsFiddle so you can check my version if you feel like it.

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.