How to create a div element in jQuery?


They are many way to add a div using jquery , but as the requirement says  on click of any square we have to addd a div.  The following code below will help in adding a div on click 

Click on any square below to see the result
 <div id='box'>    
       <div class = "div" style = "background-color:blue;"></div>
       <div class = "div" style = "background-color:green;"></div>
       <div class = "div" style = "background-color:red;"></div>
</div>
$(".div").on("click",function(){
$('#box').append(
  $('<div/>')
    .attr("id", "newDiv1")
    .addClass("div")
    .append("<span/>")
      .text("hello world")
    );
});

Updated on: 12-Jun-2020

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements