0

I have a form with a « newInput » button, which add dynamically in javascript a new input when I click to it.

When I have an error in my form, I re-load the view with the form. It’s normal, but .. Because I use dynamic javascript for adding new input, all the input added are removed when I reload..

There is something I can do ?

This is an exemple of my view.tpl :

<input type="text"  placeholder="ex: cerise" 
                    onfocus="javascript:autoComplet('jigd1', '{site_url('recettes/getIngredient')}')" value="{set_value('igd[]')}" id="jigd1" name="igd[]"/>
                            {form_error('igd[]')}

I add a partiel code of my js file

var cpt=1;
function addField(uriIngredient, uriLabel) {
try 
{
cpt=cpt+1;
var inputIgd = document.createElement('input'), 
button = document.createElement('input'),
div = document.createElement('div'),
inputIgd.setAttribute('type','text');
inputIgd.setAttribute('name','igd[]');
inputIgd.setAttribute('id','jigd'+cpt);


button.setAttribute('type','button');
button.setAttribute('onclick','javascript:supField("igd'+cpt+'")');
button.setAttribute('value','Supprimer');
div.setAttribute('id','igd'+cpt);
div.appendChild(inputIgd);
div.appendChild(button);
document.getElementById("listIgd").appendChild(div);
...
4
  • can you please write the code here... The question is not very clear Commented Dec 9, 2013 at 8:00
  • try giving us the fiddle link Commented Dec 9, 2013 at 8:01
  • You can send back all the post or get data on error to the view and recreate those elements in view. Commented Dec 9, 2013 at 8:03
  • Better you can do the form validation using javascript and ajax. Then the form will not reload. Commented Dec 9, 2013 at 8:52

1 Answer 1

1

Since you are appending new input/button to dom dynamically and not saving its state by making ajax call/submitting form you cannot retain the input/button after reloading the page.

Using localStorage, to keep the information of previously added input/buttom would be preferable.

PS : since you havent added any code which you tried, its really hard to explain localStorage with specific code.

as soon as you append, add the state of the form into localStorage, When you loading the page, look for the localStorage to check the previously added inputs you can set the item into localStorage :

window.localStorage.setItem('key','value')

You can retrieve them like this :

window.localStorage.getItem('key')
Sign up to request clarification or add additional context in comments.

1 Comment

Hey. I tried to look at what you said, that's say localStorage, and it is very nice. But I used an exemple for only one input, I don't know how to do for all inputs .. And particularly because I use javascript to add new inputs, I don't know if I can recreate all inputs ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.