2

I'm using the next JavaScript and Jquery code for to insert inputs in the html DOM:

function insertInput(elemento) {
    $(elemento).html("<input  type='text' id='"+elemento.slice(1)+"' name='"+elemento.slice(1)+"'>")
}

the parameter elemento is an id and name for the input to be generated, and the form I used is the following:

<form method="get">
    <div id="inputs">
        ... input insertion
    </div>
    <button type="submit">Aceptar</button>
</form>

and the above code, generates this

<form method="get">
    <div id="inputs">
        //example id and name
        <input type="text" id="calorias1" name="calorias1">
    </div>
    <button type="submit">Aceptar</button>
</form>

So far, everything works as it should, but, when I press the submit button, the url must be de following:

index.html?calorias1=anyvalue

instead of that:

index.html?

I don't know why that happens

  • Does it work if you use instead of a button? – Allan Wind Dec 26 '20 at 01:02
  • 1
    Does this answer your question? [Adding input elements dynamically to form](https://stackoverflow.com/questions/14853779/adding-input-elements-dynamically-to-form) (You need to create the inputs programmatically using `document.createElement()` or the jquery equivalent and append them inside the form. Merely changing the innerHTML doesn't register the new input with the form object.) – pilchard Dec 26 '20 at 01:41
  • @AllanWind placing does not work either :c. But, the button works when I put the input field in manually. thanks anyway, any help is always welcome :). – Daniel Perez Dec 26 '20 at 17:22
  • @pilchard I'll try it, and let you know if it worked. – Daniel Perez Dec 26 '20 at 17:24
  • Uh guys, for some reason the browser misinterpreted `
    ` tags, put internal elements (`
    ` and ``) outside of `` tags, I just deleted `
    ` tags in my code editor, I put'em back and it worked ( was a rare bug I had never seen that ). thank you both very much, I learned something new (`document.createElement()`).
    – Daniel Perez Dec 29 '20 at 23:45

0 Answers0