Results 1 to 3 of 3

Thread: clear input works in FF but not IE

  1. #1
    Join Date
    Apr 2008
    Posts
    28
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default clear input works in FF but not IE

    Thanks to Vic my validation check is working, I made a change to clear the form field with the wrong file type is detected, but it only clears the field in FF not IE, what have I missed for cross browser compatibility.

    Vics function including my clear command
    Code:
    <script>
    function showDiv(objectID) {
     var ip=document.getElementById('image_file'),txt=ip.value.split('.');
     var theElementStyle = document.getElementById(objectID);
     theElementStyle.style.visibility = "hidden";
     if (txt.length==2&&(txt[1]=='jpg'||txt[1]=='JPG')){
      theElementStyle.style.visibility = "visible";
     }
     else {
      document.getElementById('image_file').value = "";
      alert('File MUST be type JPG or jpg, Please Browse again for CORRECT type.');
     }
    }
    </script>
    the part I added above to clear the form file input field
    Code:
    document.getElementById('image_file').value = "";
    the form input it affects
    Code:
    SELECT NEW IMAGE&nbsp;&nbsp;<input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">
    Last edited by dragon_sa; 03-04-2012 at 07:40 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There's kinda a law against taking any kind of control over a file input. You could replace the file element with another:

    Code:
    var nfile = document.createElement('input');
    nfile.type = 'file';
    nfile.id = 'image_file';
    nfile.name = 'image_file';
    nfile.size = '35';
    nfile.accept = 'image/jpeg';
    nfile.onchange = function(){showDiv('submitDiv');return true;};
    nfile.onkeypress = function(){return nokeys(event)};
    document.getElementById('image_file').parentNode.replaceChild(nfile, document.getElementById('image_file'));
    Do that instead of:

    Code:
    document.getElementById('image_file').value = "";
    But if your file input is in a form - say:

    HTML Code:
    <form name="file_form" action="#">
    	<input name="image_file" id="image_file"" type="file" size="60" value="" />
    	<input type="hidden" name="setTime" value="<?php echo $current; ?>" />
    	<input type="hidden" name="pchange" value="Change File" />
    	<input type="submit" value="Change File" /> &nbsp;&nbsp;<b>( Changes Image )</b>
    </form>
    You can simply reset the form:

    Code:
    document.forms.file_form.reset();
    I just tested that in IE 9 and it works. And it won't affect the hidden fields because a reset will reset them to the value thay had when the page first loaded.

    But that cuts both ways. If the file input had an initial value, a reset will restore that rather than simply emptying its value.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    dragon_sa (03-04-2012)

  4. #3
    Join Date
    Apr 2008
    Posts
    28
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    Thanks John, used the reset (); and it works a treat, I even managed to add the feature of disabling the input so they cant select another file after they have chosen the correct type, and enabling it again when the form is submitted, so the value gets passed

    Heres what I ended up doing

    script
    Code:
    <script>
    function showDiv(objectID) {
     var ip=document.getElementById('image_file'),txt=ip.value.split('.');
     var theElementStyle = document.getElementById(objectID);
     theElementStyle.style.visibility = "hidden";
     if (txt.length==2&&(txt[1]=='jpg'||txt[1]=='JPG')){
      document.getElementById("chgMSG").style.display="none";
      document.getElementById("submitDiv").style.display="block";
      theElementStyle.style.visibility = "visible";
      ip.disabled = "disabled";
     }
     else {
      document.forms.pic.reset();
      alert('File MUST be type JPG or jpg, Please Browse again for CORRECT type.');
     }
    }
    function enableField() {
      document.pic.image_file.disabled = false;
    }
    </script>
    then for the form

    HTML Code:
    <form action="chgIMG.php" name="pic" method="post" enctype="multipart/form-data" target="_parent">
    	<input type="hidden" name="picID" value="<?php echo $current; ?>" />
    	<input type="hidden" name="page" value="<?php echo $page; ?>" />
    	SELECT NEW IMAGE&nbsp;&nbsp;<input name="image_file" type="file" size="35" id="image_file" accept="image/jpeg" onchange="showDiv('submitDiv');return true;" onkeypress="return nokeys(event)">
    	<br/>
    	<div id="chgMSG">New Image <b>MUST</b> be a <b>JPG</b> or <b>jpg</b> Image file type, and must also be a <b>horizontal</b> Image</div>
    	<div id="submitDiv"><input type="submit" style='vertical-align:middle;' name="submitIMG" value="Click to Upload New Image" onclick="javascript:enableField()" /></div>
    </form>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •