Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery create element containing child element

Tags:

jquery

dom

Creating a div element in jQuery does a good job on describing how to create and insert an element, however, I wish to create an element with a child element such as <li><a href="abc.html">click</a></li>. The href and text is susceptible to XSS, so I need take steps. I could create the <a> element like:

var href='abc.html',text='click';
jQuery('<a/>', {
    href: href,
    text: text
}).appendTo('#mySelector');

How do I modify this to include the </li> element?

like image 726
user1032531 Avatar asked Feb 10 '26 14:02

user1032531


1 Answers

wrap it up:

$(document).ready(function() {
  var href='abc.html',text='click';

jQuery('<a/>', {
    href: href,
    text: text
}).wrap("<li>").parent().appendTo('#mySelector');


})

http://codepen.io/anon/pen/Eyqco

like image 84
LorDex Avatar answered Feb 13 '26 13:02

LorDex