html - How to delete the row in a div without using table in javascript -
hi guys source code need delete row in div when press delete button using javascript please give me solution me.the important thing don't use table instead of div .so please give me solution me.
function add() { var x = document.queryselectorall(".div1"); var i; (i = 0; < x.length; i++) { x[i].innerhtml += "<br><br> <input type='text' name='mytext'>"; } } function del() { var y = document.queryselectorall(".div2"); var i; (var = 0; < y.length; i++) { y[i].innerhtml += "<br><br><br> <input type='button' value='delete' onclick='removerow(this)'>"; } } function removerow(input) { input.parentnode.removechild(input.previoussibling); input.parentnode.removechild(input); }
#add_btn { float: left; margin: 0% 0% 0% 72%; border-radius: 25px; cursor: pointer; padding: 10px; } body { background: #00ffff; } input[type=text] { padding: 5px; margin: 5px 0px 25px 0px; border: 2px solid #ccc; border-radius: 5px; } input[type=button] { padding: 5px; border: 2px solid #ccc; border-radius: 5px; background: #00ff99; cursor: pointer; } #wrapper { width: 80%; margin: 7% auto; } .div1 { float: left; width: 20%; background: #4dffc3; } .div2 { float: left; width: 20%; background: lightyellow; } #div3 { float: left; width: 20%; background: lightgray; } #div4 { float: left; width: 20%; background: lightblue; } #clearfix { clear: both; }
<section id="wrapper"> <button id="add_btn" onclick="add(); del();">add textbox</button><br><br> <div class="div1"> <p>this div one</p> </div> <div class="div1"> <p>this div two</p> </div> <div class="div1"> <p>this div threee</p> </div> <div class="div1"> <p>this div four</p> </div> <span class="div2"> <p>this div five</p> </span> <!--<div id="clearfix"></div>--> </section>
you must create class each line , remove class,
see example below how reach
var cpt = 0; function add() { var x = document.queryselectorall(".div1"); var i; (i = 0; < x.length; i++) { x[i].innerhtml += "<br><br> <input type='text' name='mytext' class='"+cpt+"'>"; } } function del() { var y = document.queryselectorall(".div2"); var i; for(var i=0;i<y.length;i++) { y[i].innerhtml += "<br><br><br> <input type='button' value='delete' class='"+cpt+"' onclick='removerow("+cpt+")'>"; } cpt++; } function removerow(input) { var list = document.getelementsbyclassname(input); for(var = list.length - 1; 0 <= i; i--) if(list[i] && list[i].parentelement) list[i].parentelement.removechild(list[i]); }
#add_btn { float:left; margin:0% 0% 0% 72%; border-radius: 25px; cursor:pointer; padding:10px; } body { background: #00ffff; } input[type=text] { padding: 5px; margin:5px 0px 25px 0px; border: 2px solid #ccc; border-radius: 5px; } input[type=button] { padding: 5px; margin:5px 0px 25px 0px; border: 2px solid #ccc; border-radius: 5px; background:#00ff99; cursor:pointer; } #wrapper { width:80%; margin:7% auto; } .div1 { float:left; width:20%; background:#4dffc3; } .div2 { float:left; width:20%; background:lightyellow; } #div3 { float:left; width:20%; background:lightgray; } #div4 { float: left; width:20%; background:lightblue; } #clearfix { clear:both; }
<section id="wrapper"> <button id="add_btn" onclick="add(cpt); del(cpt);">add textbox</button> <div class="div1"> <p>this div one</p> </div> <div class="div1"> <p>this div two</p> </div> <div class="div1"> <p>this div threee</p> </div> <div class="div1"> <p>this div four</p> </div> <span class="div2"> <p>this div five</p> </span> <!--<div id="clearfix"></div>--> </section>
Comments
Post a Comment