input file question

Discussion in 'Javascript' started by Federico, Oct 14, 2004.

  1. Federico

    Federico Guest

    Hello,

    I can't find how to do this: I have an input of type=file and I want
    that when the user clicks on it, javascript checks if some checkbox is
    selected and if so, the file dialog is shown. If not, nothing is shown.

    Any idea?
    thanks in advance,
    Federico
     
    Federico, Oct 14, 2004
    #1
    1. Advertising

  2. Federico

    Grant Wagner Guest

    Federico wrote:

    > Hello,
    >
    > I can't find how to do this: I have an input of type=file and I want
    > that when the user clicks on it, javascript checks if some checkbox is
    > selected and if so, the file dialog is shown. If not, nothing is shown.
    >
    > Any idea?
    > thanks in advance,
    > Federico


    Can't be done in a reliable, cross-browser, cross-platform way. You can't
    make the file dialog open, on some operating systems, with some browsers,
    there is no "file dialog". On other browsers and other platforms, there is
    simply no way to achieve the desired affect.

    <script type="text/javascript">
    function testAndOpen(fi) {
    var v;
    if (fi &&
    (v = fi.form) &&
    (v = v.elements) &&
    (v = v['autoOpen']) &&
    v.checked &&
    ((v = typeof fi.click) == 'function' ||
    (v == 'object' && v != null))) {

    fi.click();
    }
    }
    </script>
    <form>
    <input type="file" onfocus="testAndOpen(this, this.form);">
    <input type="checkbox" name="autoOpen" checked="checked">
    </form>

    Works in: IE 6.0.2800 (on Windows XP)
    Does not work in: Firefox 1.0PR, Netscape 4.78, Opera 6.05, Opera 7.54,
    Mozilla 1.7.3 (on Windows XP)

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Oct 14, 2004
    #2
    1. Advertising

  3. Federico

    Lee Guest

    Federico said:
    >
    >Hello,
    >
    >I can't find how to do this: I have an input of type=file and I want
    >that when the user clicks on it, javascript checks if some checkbox is
    >selected and if so, the file dialog is shown. If not, nothing is shown.


    Instead, disable the file input unless the box is checked:

    <form>
    <input name="fileInput"
    type="file"
    disabled="true">
    <br>
    <input type="checkbox"
    onclick="this.form.fileInput.disabled=!checked">
    </form>
     
    Lee, Oct 14, 2004
    #3
  4. Federico

    Federico Guest

    Lee wrote:
    > Federico said:
    >
    >>Hello,
    >>
    >>I can't find how to do this: I have an input of type=file and I want
    >>that when the user clicks on it, javascript checks if some checkbox is
    >>selected and if so, the file dialog is shown. If not, nothing is shown.

    >
    >
    > Instead, disable the file input unless the box is checked:
    >
    > <form>
    > <input name="fileInput"
    > type="file"
    > disabled="true">
    > <br>
    > <input type="checkbox"
    > onclick="this.form.fileInput.disabled=!checked">
    > </form>


    I already did so, but in Safari doesn't work. That's the reason I was
    looking for what I asked.

    Thanks to both of you. Finally, I leaved it as impossible and tried to
    convince myself that it was not so important.
     
    Federico, Oct 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,710
  2. ashutosh
    Replies:
    3
    Views:
    9,053
    Andrew Thompson
    Jun 16, 2005
  3. Guy
    Replies:
    5
    Views:
    2,874
    brucie
    Dec 13, 2003
  4. Replies:
    3
    Views:
    415
  5. Tarkeshwar
    Replies:
    2
    Views:
    210
Loading...

Share This Page