javascript - how to allow drag and drop in multiple divs using html5 -
am trying build form using html5.. have 1 div contains list of fields , and div can put fields build form.. on dropping text name respective text field , label drooped in div.. want create 2 divs can put fields in 2 divs can 1 tell how using code working 1 div
<html> <head> <title>drag , drop</title> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript"> var i=0; function allowdrop(ev) { ev.preventdefault(); } function drag(ev) { ev.datatransfer.setdata("text",ev.target.id); } function drop(ev) { i++; ev.preventdefault(); var data=ev.datatransfer.getdata("text"); //alert(data); var field_type=field_set(data); var output=document.getelementbyid("div1"); if(!document.getelementbyid('drag'+i)) { var ele = document.createelement("div"); ele.setattribute("id","drag"+i); ele.setattribute("class","inner"); //var val=field_type; //alert(val); ele.innerhtml=field_type; output.appendchild(ele); } } function field_set(data) { if(data=='name') { var label=prompt("please enter field name",""); if(label=='') { var val=''; } else { var field=label+"<input type='text' id='text1' class='text1'>"; // alert('field'); } } if(data=='pass') { var field="<input type='password' id='text1' class='text1'>"; } return field; } </script> </head> <div id="container"> <h3 align="center">form builder</h3> <div id="left"> <div id="l_content"> <h3>field elements</h3> <div id="name" draggable="true" ondragstart="drag(event)" class="drag1"> <label id="name1">name</label> <!--<input type="text" id="text1" class="text1">--> </div> <div id="pass" class="drag1" draggable="true" ondragstart="drag(event)"> <label>password</label> <!-- <input type="password" id="text2" class="text1">--> </div> </div> </div> <div id="right"> <h3>form</h3> <div id="div1" class="div1" ondrop="drop(event)" ondragover="allowdrop(event)"> </div> </div> <div id="btn"><input type="submit" value="create form" ></div> </div> </div> </html>
pass id of div along drop function.
and
function drop(ev,did) { //var output=document.getelementbyid("div1"); var output=document.getelementbyid(did); }
you ge result...
Comments
Post a Comment