tags:

views:

60

answers:

4

Everything below works fine, except that the form doesnt get submitted... it seems the problem lies in the 'x.onchange' event... any tips?

 var form = document.forms['pic_form'];
 var x=document.createElement("input");
 x.type="file";
 x.name="pic_file2";
 x.id="pic_file2";
 x.size="35";
 x.onchange="pic_form_function(form, 1);";
 var z=document.getElementById("pic_file2");
 z.parentNode.replaceChild(x, z);

here is the function called:

    pic_form_function(formName, nr){ if (nr==1){formName.submit(); }}

It wont submit, but it DOES get replaced and all... help please!

+1  A: 

How about:

x.onchange = function(){
               pic_form_function(form, 1);
             };

for I never trust assigning events with strings.

thephpdeveloper
+4  A: 

Try replacing

x.onchange="pic_form_function(form, 1);";

with

x.onchange = function () { pic_form_function(form, 1); }

which is a

function expression (function operator)

A function expression is similar to and has the same syntax as a function declaration

function [name]([param] [, param] [..., param]) {
   statements
}

name

The function name. Can be omitted, in which case the function becomes known 
as an anonymous function.

param

The name of an argument to be passed to the function. A function can have 
up to 255 arguments.

statements

The statements which comprise the body of the function.

Also read

Javascript anonymous functions

rahul
A: 

You are missing the keyword function before your declaration this should read

function pic_form_function(formName, nr){ if (nr==1){formName.submit(); }}

Check here for a working example

http://jsbin.com/uvehe

jitter
A: 

Try

x.onchange="pic_form_function(document.forms['pic_form'], 1);";
Himadri