Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create nested divs with jquery

Tags:

html

jquery

I have to dinamically create nested divs using jquery .append()

But by this code:`

html=$("<div class='table'>")
    .append($("<div class='row'>"))
    .append($("<label>Denominazione Gruppo</label>"))
    .append($("<input type='text' id='denominazione'>"));
$("#content").empty();
$("#content").append(html);`

i get this wrong output

<div id="content">
    <div class="table">
        <div class="row"></div>    //this closed tag should not be here!
        <label>Denominazione Gruppo</label>
        <input type="text" id="denominazione">
    </div>
</div>

Where i wrong? Thank you.

like image 478
ZioTano Avatar asked Jan 21 '26 09:01

ZioTano


1 Answers

You need to append the input and label to the row element

html = $("<div class='table'>").append($("<div class='row'>")
    .append("<label>Denominazione Gruppo</label>")
    .append("<input type='text' id='denominazione'>"));
$("#content").empty();
$("#content").append(html);

Demo: Fiddle

like image 186
Arun P Johny Avatar answered Jan 23 '26 21:01

Arun P Johny