0

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>

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

4 Comments

Got it! i also declared a variable for the icon this.$element.append('<a href="#"><i class="fa '+icon+'"></i></a>'), thanks!
Hey, could you mark as solution, by chance, it'd help a lot. And just a heads up that will return $element, instead do $('<a href="#"><i class="fa '+icon+'").appendTo($element) if you want the new thing.
Is fa- included inside the icon? Additionally, it will only not display (but will be in Chrome inspector) if there is no valid icon, so, that means you probably aren't inputting the right icon name
Hi i just got it, there was an error displaying the icon what i did was fix it and the link work properly, thanks again!
0
$("#id").on("click",function(ev){
    ev.preventDefault();
    $("body").append('<a href="#"><i class="fa fa-list"></i></a>');
})

Comments

0

$('#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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.