I've seen that you can dynamically add HTML with jquery:
$( "div" ).append( "<p>Test</p>" );
What would be the way to dynamically add somewhat more complicated HTML:
<blockquote class="col-sm-9">
<p class="lead">Lorem Ipsum</p>
<footer>Lorem Ipsum <cite title="Source Title">Lorem Ipsum</cite></footer>
</blockquote>
3 Answers 3
this is how I normally do it (simple and readable), unless there is an id in the markup
var html = '<blockquote class="col-sm-9">';
html += '<p class="lead">Lorem Ipsum</p>';
html += '<footer>Lorem Ipsum <cite title="Source Title">Lorem Ipsum</cite></footer>';
html += '</blockquote>'
$( "div" ).append( html );
if you also want to assign unique id while appending it then
var blockQuoteCounter = 0;
var html = '<blockquote class="col-sm-9" id="blockQuote_' + blockQuoteCounter + '">';
html += '<p class="lead">Lorem Ipsum</p>';
html += '<footer>Lorem Ipsum <cite title="Source Title">Lorem Ipsum</cite></footer>';
html += '</blockquote>'
$( "div" ).append( html );
Comments
<html>
<head>
<title>sample Page</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$('#btnAdd').click(function () {
var szData = "";
szData = '<blockquote class="col-sm-9"> <p class="lead">Lorem Ipsum</p>';
szData = szData + '<footer>Lorem Ipsum <cite title="Source Title">Lorem Ipsum</cite>';
szData = szData + '</footer></blockquote>';
$('div').append(szData)
});
});
</script>
</head>
<body>
<input type="button" value="Add Data" id="btnAdd"/>
<div></div>
</body>
</html>
Comments
this is a answer for how to display html code in javascript
$( "#update" ).append( 'You have 5 tokens remaining for this coupon. Would you like to Update the unused tokens? Please select quantity you would like to Update."{{ csrf_field() }} Update Coupon Quantity. Recover Unused Coupon.Close');
var Your_HTML=$(".ur_hidden_div_class").html();and than append it whereever