input file question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Federico

    #1

    input file question

    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
  • Grant Wagner

    #2
    Re: input file question

    Federico wrote:
    [color=blue]
    > 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[/color]

    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="testAn dOpen(this, this.form);">
    <input type="checkbox" name="autoOpen" checked="checke d">
    </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 <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Lee

      #3
      Re: input file question

      Federico said:[color=blue]
      >
      >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.[/color]

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

      <form>
      <input name="fileInput "
      type="file"
      disabled="true" >
      <br>
      <input type="checkbox"
      onclick="this.f orm.fileInput.d isabled=!checke d">
      </form>

      Comment

      • Federico

        #4
        Re: input file question

        Lee wrote:[color=blue]
        > Federico said:
        >[color=green]
        >>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.[/color]
        >
        >
        > Instead, disable the file input unless the box is checked:
        >
        > <form>
        > <input name="fileInput "
        > type="file"
        > disabled="true" >
        > <br>
        > <input type="checkbox"
        > onclick="this.f orm.fileInput.d isabled=!checke d">
        > </form>[/color]

        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.

        Comment

        Working...