Unable to dynamically create div content


user7461846

What's the problem here?

var str = "
<div class='lorem'>lorem</div>
<br>
<div class='ipsum'>ipsum</div>
";

$('.parent').html(str);
.parent{
width:50%;
margin:14px auto;
background:#eee;
}

.lorem{
cursor:cell;
background:gold;
}

.ipsum{
cursor:cell;
background:lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='parent'>

</div>

mistake:

Uncaught SyntaxError: Invalid or unexpected token...

web page hit counter

Baruch

There must be no newlines in the string unless template literals are used.

var str = "
  <div class='lorem'>lorem</div>
  <br />
  <div class='ipsum'>ipsum</div>
";

should

const str = `
  <div class='lorem'>lorem</div>
  <br />
  <div class='ipsum'>ipsum</div>
`;

Reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals _

If you don't want to use it (I don't know why you don't), you can also do:

var str = "<div class='lorem'>lorem</div>" + "<br />" + "<div class='ipsum'>ipsum</div>";

var str = ["<div class='lorem'>lorem</div>", "<div class='ipsum'>ipsum</div>".join("<br />");

var str = `
<div class='lorem'>lorem</div>
<br>
<div class='ipsum'>ipsum</div>
`;

$('.parent').html(str);
.parent{
width:50%;
margin:14px auto;
background:#eee;
}

.lorem{
cursor:cell;
background:gold;
}

.ipsum{
cursor:cell;
background:lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='parent'>

</div>

Related


Dynamically create div with htmlhelpers?

Dillashaw I have a problem with my MVC application as shown. So when I make a click on the checkbox, the script will be set divvisible visible. But this is not a good idea because there is an empty space above the second one .div Checkbox with script indie:

Create Div and insert dynamically

curiosity I have the following code: <div id="one"></div> <div id="two"></div> <div id="three"></div> $('<p />', { 'text': 'My Products' }).appendTo('#one'); $('<li />').appendTo('#one'); How can I dynamically create <div id="one">and insert it before t

Dynamically create div with htmlhelpers?

Dillashaw I have a problem with my MVC application as shown. So when I make a click on the checkbox, the script will be set divvisible visible. But this is not a good idea because there is an empty space above the second one .div Checkbox with script indie:

Create Div and insert dynamically

curiosity I have the following code: <div id="one"></div> <div id="two"></div> <div id="three"></div> $('<p />', { 'text': 'My Products' }).appendTo('#one'); $('<li />').appendTo('#one'); How can I dynamically create <div id="one">and insert it before t

Dynamically create div with htmlhelpers?

Dillashaw I have a problem with my MVC application as shown. So when I make a click on the checkbox, the script will be set divvisible visible. But this is not a good idea because there is an empty space above the second one .div Checkbox with script indie:

Create <div> and append <div> dynamically

User 1323595: I am trying to create <div>dynamically , and append <div>content to it. What I have so far is this: var iDiv = document.createElement('div'); iDiv.id = 'block'; iDiv.className = 'block'; document.getElementsByTagName('body')[0].appendChild(iDiv);

Create <div> and append <div> dynamically

User 1323595: I am trying to create <div>dynamically , and append <div>content to it. What I have so far is this: var iDiv = document.createElement('div'); iDiv.id = 'block'; iDiv.className = 'block'; document.getElementsByTagName('body')[0].appendChild(iDiv);

Dynamically change div content with jQuery

Camillon I would like to know how to change the html content of the div. Here is the starting point: <div id="container"> <div class="fruits">apple</div> <div class="fruits">banana</div> <div class="fruits">strawberry</div> </div> Page output: App

Copy div dynamically with dynamic content

King Alfred Chameleon I have a div called "red" that displays messages sent by users on my webpage. The problem is that "red" is only 40px long and the message usually goes through the div. I want to add another copy of the "red" div on the webpage after the m

Dynamically change div content with jQuery

Camillon I would like to know how to change the html content of the div. Here is the starting point: <div id="container"> <div class="fruits">apple</div> <div class="fruits">banana</div> <div class="fruits">strawberry</div> </div> Page output: App

jQuery dynamically add content to div

Ron What I want to do is add the content of each item in the xml file to the html page. I'm trying to make the content of each item inside a <article>label. However, this is my first time using jQuery and the end result is not what I want. let $items = $(xmlCo

Dynamically add a div and its content

user7461846 <div class='tagstore' id='tagstore'> <div class='tagsingle' title='delete'>323</div> <div class='tagsingle' title='delete'>525</div> </div> I need to add the following: <div class='tagsingle' title='delete'>some variable</div> js var a = '

Dynamically change div content with jQuery

Camillon I would like to know how to change the html content of the div. Here is the starting point: <div id="container"> <div class="fruits">apple</div> <div class="fruits">banana</div> <div class="fruits">strawberry</div> </div> Page output: App

jQuery dynamically add content to div

Ron What I want to do is add the content of each item in the xml file to the html page. I'm trying to make the content of each item inside a <article>label. However, this is my first time using jQuery and the end result is not what I want. let $items = $(xmlCo

How to dynamically change div content

username I was looking for "change div content in php without refreshing the page", I found the following and applied it: <script> $(document).ready(function(){ $('#wwage').submit(function() { $.ajax( { data: $(this).serialize(), type:

Copy div dynamically with dynamic content

King Alfred Chameleon I have a div called "red" that displays messages sent by users on my webpage. The problem is that "red" is only 40px long and the message usually goes through the div. I want to add another copy of the "red" div on the webpage after the m

Dynamically change div content with jQuery

Camillon I would like to know how to change the html content of the div. Here is the starting point: <div id="container"> <div class="fruits">apple</div> <div class="fruits">banana</div> <div class="fruits">strawberry</div> </div> Page output: App

jQuery dynamically add content to div

Ron What I want to do is add the content of each item in the xml file to the html page. I'm trying to make the content of each item inside a <article>label. However, this is my first time using jQuery and the end result is not what I want. let $items = $(xmlCo

Dynamically change div content with jQuery

Camillon I would like to know how to change the html content of the div. Here is the starting point: <div id="container"> <div class="fruits">apple</div> <div class="fruits">banana</div> <div class="fruits">strawberry</div> </div> Page output: App

How to dynamically change div content

username I was looking for "change div content in php without refreshing the page", I found the following and applied it: <script> $(document).ready(function(){ $('#wwage').submit(function() { $.ajax( { data: $(this).serialize(), type:

jQuery dynamically add content to div

Ron What I want to do is add the content of each item in the xml file to the html page. I'm trying to make the content of each item inside a <article>label. However, this is my first time using jQuery and the end result is not what I want. let $items = $(xmlCo

Dynamically create QML ListElement and content

IQ So I am trying to dynamically create ListElements ListModelin . This worked fine until I tried to write some content in ListElements to be loaded dynamically. I tried making my own file with ListElementhours and hours as properties , but the model gets an e

Dynamically create QML ListElement and content

IQ So I am trying to dynamically create ListElements ListModelin . This worked fine until I tried to write some content in ListElements to be loaded dynamically. I tried making my own with ListElementin and hour as properties , but the model then gives an erro

Dynamically create QML ListElement and content

IQ So I am trying to dynamically create ListElements ListModelin . This worked fine until I tried to write some content in ListElements to be loaded dynamically. I tried making my own file with ListElementhours and hours as properties , but the model gets an e

Create a lightbox with dynamically generated content?

knock loose I'm building a twitter fetcher and by working with prototypes I've realized that scrolling is very unwieldy due to some large images. I would like to restrict images in the same way as Twitter: But I'm not sure how to do it. I know I'm going to nee

Dynamically create QML ListElement and content

IQ So I am trying to dynamically create ListElements ListModelin . This worked fine until I tried to write some content in ListElements to be loaded dynamically. I tried making my own with ListElementin and hour as properties , but the model then gives an erro

Dynamically create QML ListElement and content

IQ So I am trying to dynamically create ListElements ListModelin . This worked fine until I tried to write some content in ListElements to be loaded dynamically. I tried making my own file with ListElementhours and hours as properties , but the model gets an e

Unable to dynamically create table in MySQL

username I am following a model view control approach to create an application. I am trying to dynamically create a table when the page is loaded. Here is the code. existdashboard.php <?php require_once ("controller/db-config.php"); require_once ("contro

Dynamically change content of div and append to another div

international student So basically I have this div in my body <body> <div id="main_content"></div> </body> Now, I downloaded some data (a set of boolean data) from the internet and I have this div template. Suppose the data is (true, false, true). Then for e