Writing HTML with jQuery

I am new to jQuery. I want to write the following HTML (along with classes) using jQuery. How can i do this?

<div class="phnbr"> <div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a> </div> </div> 
+6
source share
3 answers
 $('<div class="phnbr"> \ <div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a> \ </div> \ </div>'); // bang done! 
+7
source
 $('<div>').addClass('phnbr').append($('<div>').addClass('phtext').append('hi how are you, ').append($('<a>').attr({ target: '_blank', href: 'http://www.xyc.com'}).text('click here.'))); 
+16
source

Well, you can just use append () or another DOM Insertion Function

 $(document.body).append('<div class="phnbr"><div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a></div></div>'); 
+5
source

Source: https://habr.com/ru/post/885486/


All Articles