Wednesday, April 6, 2011

click() not working on FireFox

<script>
function attachFile(){
  document.getElementById("file").click(); 
  return;
}
</script>

<form name="novsh" style="margin: 0px; padding: 0px;">
<input type="file" name="file" style="display:none" id="file" />
</form>

<a href="javascript:attachFile()">Attach File</a>
<input type="button" name="btn" value="click me" onclick="attachFile()"/>

I've written like this and it's working on IE but not working on FireFox

From stackoverflow
  • The click() function on elements that aren't form inputs is a non-standard feature that only exists in IE.

    If you're trying to call the element's onclick handler, you can do this:

    document.getElementById("file").onclick();
    

    Edit:

    Turns out this is a form input, so...never mind.

    But it looks like what you're trying to do is get the file picker dialog to pop up. I'm not aware of any programmatic way to do that. I would agree with RoToRa's comment that such a capability is probably a security flaw.

    If your ultimate goal is to create a file input element that can be styled differently from the browser's defaults, check out this article.

    T.J. Crowder : For completeness: That will call the DOM0-style handler (which is what he has). Any DOM2-style handlers will not get called.
    Pointy : I was going to write that too, but I checked the Mozilla documentation and wrote a test file, and there is indeed a "click" function on input elements in Firefox.
    T.J. Crowder : @Pointy: He did say "...that aren't form inputs..."
    Pointy : oh durr well I haven't had enough coffee apparently. However the question does in fact seem to have to do with inputs, so there is a "click" to call. Firefox doesn't obey that, however, for inputs of type "file" as far as I can tell.
    T-joe : Thank you guys.
  • Firefox does not allow you to programatically call click() on a file input.

  • This article achieves the same in a different way Styling an input type="file"

0 comments:

Post a Comment