Reputation: 912
Just want to ask how to create a element using jquery i am able to create a href using below code.
document.createElement("a");
How can i create a element inside another element , exactly below.
<a href="#"><i class="fa fa-list"></i></a>
Upvotes: 0
Views: 39
Reputation: 154
$('#main').after().append('<a href="#"><i class="fa fa-list"></i></a>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" integrity="sha384-dNpIIXE8U05kAbPhy3G1cz+yZmTzA6CY8Vg/u2L9xRnHjJiAK76m2BIEaSEV+/aU" crossorigin="anonymous">
<div id="main">
</div>
Upvotes: 0
Reputation: 252
$("#id").on("click",function(ev){
ev.preventDefault();
$("body").append('<a href="#"><i class="fa fa-list"></i></a>');
})
Upvotes: 0
Reputation: 14313
You can use the append function with jQuery.
$("#add").click(function() {
$('<a href="#"><i class="fa fa-list"></i></a>').appendTo(document.body);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css" integrity="sha384-dNpIIXE8U05kAbPhy3G1cz+yZmTzA6CY8Vg/u2L9xRnHjJiAK76m2BIEaSEV+/aU" crossorigin="anonymous">
<button id="add">Add Stuff</button>
Upvotes: 1