Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an element inside a div using jQuery?

Tags:

jquery

element

How do I write a <div> inside another with jQuery?

I have a <div>that I can not modify in HTML because I am working in a CMS. So I want to write an element (<div>) inside this <div> with a click function.

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

like image 552
DiegoP. Avatar asked May 19 '11 21:05

DiegoP.


3 Answers

You could select the existing div, and append a new div to it:

$('#OuterDiv').append('<div id="innerDiv"></div>');
like image 110
Andomar Avatar answered Nov 18 '22 17:11

Andomar


Like this:

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

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

});

or this:

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

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

    $(this).append($div);

});
like image 34
Code Maverick Avatar answered Nov 18 '22 17:11

Code Maverick


$('.clickaclick').click( function(){    
    $('.parent_div').html('<div />');
});

That should do it, assuming you don't care what's in that .parent_div . There are so many approaches to doing this kind of thing, it'd do you best to read up a bit on jQuery's DOM insertion methods.

http://api.jquery.com/html/

http://api.jquery.com/append/

http://api.jquery.com/appendTo/

http://api.jquery.com/prepend/

http://api.jquery.com/prependTo/

http://api.jquery.com/text/

like image 4
dclowd9901 Avatar answered Nov 18 '22 16:11

dclowd9901