Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » JavaScript: Adding INPUTs onsubmit

This thread is locked; no one can reply to it. rss feed Print
JavaScript: Adding INPUTs onsubmit
Billybob
Member #3,136
January 2003

I need to add an input to an HTML form when the user clicks the Submit button:

1<script src='jquery.js'></script>
2<script>
3function form_special_file_input()
4{
5 $('input.special_file_input').each( function (i) {
6 alert(this.value);
7 var input = document.createElement('input');
8 input.value = this.value;
9 input.name = this.name;
10 input.type = 'hidden';
11 input.class = '';
12 document.forms[0].appendChild(input);
13 alert(i);
14 });
15
16 alert('done');
17
18 return false;
19}
20</script>

According to Dom Inspector the element is added. But it doesn't get POST'd. What's the deal?

Matthew Leverton
Supreme Loser
January 1999
avatar

You should set the type first before any other parameters. Browsers, especially Opera, are picky about that sort of thing. input.class should be input.className.

Adding things during the onsubmit phase of a form can be tricky. Browsers handle it differently. It might be better to try to set a timeout that submits the form a few ms after the field is added.

Billybob
Member #3,136
January 2003

Yeah, I'm having serious trouble getting it to see the field. I tried your suggestion of setting the type first, to no avail. I added a new button. One adds the field. The other submits the form. So even with a delay it doesn't work.
And finally I tried a div and modifying its innerHTML. Same result.

In this particular case I've decided to go with XMLhttprequest, using the ajax extension to JQuery. Still wish I could add fields, but oh well not worth my time figuring out right now.

Thank you for the help.

EDIT: Dangit, now I gotta figure out how to shove files from file inputs across XMLhttprequest >:(
EDIT2: Nevermind, don't really need to do that (and it appears to be impossible).

Go to: