How to write an element inside a div using jQuery?

How to write <div>inside another using jQuery?

I have <div>one that I can’t change in HTML, because I work in CMS. So I want to write an element ( <div>) inside this <div>with a click function.

I already created the click function, but how do I write using jQuery a <div>INSIDE another specific one <div>?

+3
source share
5 answers

You can select an existing div and add a new div to it:

$('#OuterDiv').append('<div id="innerDiv"></div>');
+9
source

Like this:

$("#div1").click(function() {

    $(this).append("<div>new div</div>");

});

or that:

$("#div1").click(function() {

    var $div = $("<div/>")
                   .attr("id", "div2")
                   .html("new div");

    $(this).append($div);

});
+2
source

You can use the jQuery wrap () method, which will wrap you with an inner div inside another http://api.jquery.com/wrap/

0
source

create new item

$('<div>')

then add this where you need

$('<div>').text('im a new div').appendTo($('#divId'));

hope this helps

0
source

All Articles