0

I am trying to insert a element in a form with jQuery.

To do this, I clone the element, change the attribute and insert, then use .html() to convert the jquery object to HTML object but it doesn't work.

function addInput(divName){
    var selector= $('#tipoProducto').clone();
    selector.attr('name', counter );

    var newdiv = document.createElement('div');
    newdiv.innerHTML = "<li>porcentaje "
       + (counter + 1) 
       + "</li><li>\
         <input type='text' name='myInputs[]'></li>\
         <li>Ingediente Activo" 
       + (counter + 1)
       + "</li><li>"
       + selector.html()
       + "</li>";
    document.getElementById(divName).appendChild(newdiv);
    counter++;
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
user852610
  • 2,205
  • 7
  • 36
  • 46

1 Answers1

4

For that, I clone the element change the attribute and insert then use .html() to convert the jquery object to html object but dont work.

You don't have to do that. You can do something like this instead, more generically:

var div = $('<div><ul><li>X</li><li>Y</li></ul></div>');
$(div).appendTo($('#' + divName));

No need to use html(), DOM code, etc. like that. In fact, you should do it that way because jQuery's DOM manipulation shortcuts are 99.99% cross browser (maker and version) whereas standard DOM calls can be different in older versions of IE.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83