Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write HTML code with JavaScript (jquery)

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>
like image 465
Diego Avatar asked Oct 15 '25 03:10

Diego


1 Answers

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 );
like image 73
gurvinder372 Avatar answered Oct 17 '25 09:10

gurvinder372



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!