javascript - How can I remove the input before cloning a div? -


i've found few answers question on stack overflow, it's not working me. @ moment, code looks (i removed other divs it's not long read) :

<div id="dynamicinput">    <div id="duplicater">       <input type="text" placeholder="event title" name="title">       <input type="text" placeholder="url" name="url">    </div> </div> 

and javascript :

  var = 0;   var original = document.getelementbyid('duplicater');    function duplicate() {     var clone = original.clonenode(true);     clone.id = "duplicate" + ++i;     original.parentnode.appendchild(clone); } 

it works great, if put text before pressing button "add event", clones text, , don't want that. i've tried add .find('input').val('') on first line, it's not working, can't clone anymore. error in console method find not found.

the same thing happens solutions i've found on forum : or disabled button , can't duplicate anymore, or works clones text.

how can this?

thanks !!

you can loop through elements in div , clear them individually.

var = 0;    var original = document.getelementbyid('duplicater');    var onclick = function() {      var clone = original.clonenode(true);      clone.id = "duplicate" + ++i;            (var = 0; < clone.childnodes.length; i++) {        var e = clone.childnodes[i];        if (e.tagname) switch (e.tagname.tolowercase()) {           case 'input':              switch (e.type) {                 case "radio":                 case "checkbox": e.checked = false; break;                 case "button":                 case "submit":                 case "image": break;                 default: e.value = ''; break;              }              break;           case 'select': e.selectedindex = 0; break;           case 'textarea': e.innerhtml = ''; break;           default: break;        }     }      original.parentnode.appendchild(clone);  };    $('#button').click(onclick);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>  <div id="dynamicinput">     <div id="duplicater">        <input type="text" placeholder="event title" name="title">        <input type="text" placeholder="url" name="url">     </div>  </div>  <input type="button" id="button" value="clone" />


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -