1

I have a function i would like to run onChange. This works fine with an a-tag. But when i use it on an input-tag it just says:

form.saveFile is not a function

This works:

<a onclick="form.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest'); return false;" href="#">Upload file</a>

This does NOT work:

<input onchange="form.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest');" id="fec056b774ffefa479c7dd3a632275cb_filetest" class="file" type="file"  name="fec056b774ffefa479c7dd3a632275cb_filetest" accesskey="F"/>

Both calls the same form.saveFile(); function.

The form variable is declared in a included js file like this:

form = {
  version: '1.0.0',
  ...alot of functions... 
  saveFile : function(callback,p){
    .................
  }
}
3
  • 2
    Where does the form variable come from? Where did you declare it? Commented Nov 8, 2009 at 10:12
  • Please post a minimal snipped of html + javascript code that reproduces the problem (both the working a onclick and the non-working input onchange). Commented Nov 8, 2009 at 10:18
  • I did post 2 html snippets. One working and one not working. a onclick works. but input onchange says the function is undefined. Commented Nov 8, 2009 at 10:20

3 Answers 3

3

form is a reserved word in a onchange handler. Just rename your variable and it will work:

var frm = {
    version: '1.0.0',
    saveFile : function(callback,p){
    }
};

<input onchange="frm.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest');" id="fec056b774ffefa479c7dd3a632275cb_filetest" class="file" type="file"  name="fec056b774ffefa479c7dd3a632275cb_filetest" accesskey="F"/>
Sign up to request clarification or add additional context in comments.

Comments

1

Inside forms form does refer to that specific form element. So you need to distinguish between the form that’s referring to the form element and your form variable. You can do so with using window.form if you defined your form variable in the global scope. Or you just rename it.

Comments

0

need more info. did you try in all browsers? it should work. If the text has been changed, once the focus leaves the text box onchange will occur. try using alert() to test if the function is really called. there could be something wrong with your function. hope this helps.

2 Comments

I tried in most browsers. First one was firefox. Alert(1); in both a-tag and input-tag works fine. But i cannnot access form.saveFile from the input-onchange. Only from a-onclick
try using document.getElementById('form_id'). since ids are unique, you wont get any problem when referring to elements in html page.

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.