1

Using a Jquery plugin called filestyle from: http://www.appelsiini.net/projects/filestyle. I have a select box with an id of adtype. If the selected option has a value of 7, it'll disable the file input with the id of 'adfile'.

The problems becomes when a user has already selected a file. I cant find a way to clear the file input and then disable it. $('#adfile').val(''); does not work.

JQUERY

$(function(){
  $('#adtype').change(function(){
    if($('#adtype option:selected').val()==="7"){
      $('#adfile').val('');
      $("#adfile").prop('disabled', true);
    }else{
      $("#adfile").prop('disabled', false);
    }
  });
});

HTML

<form id="someform" action="somepage" method="post">
  <select name="selectstuff" id="adtype">
    <option value="1">one</option>
    <option value="5">five</option>
    <option value="7">seven</option>
  </select>

  <input type="file" id="adfile" value="" />

  <input type="submit" value="submit" />
</form>
0

4 Answers 4

6

I used $('#adfile').val(''); and $('#adfile').attr('disabled', 'disabled');

DEMO

NOTE: filestyle plugin removes id,name attributes from the file input element and adds file as class. I'm accessing this element with reference to the form.

Hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

I'm accepting your answer, although it wont work on my page. I believe the filestyle plugin from: appelsiini.net/projects/filestyle is interfering with clearing the file input field. If I remove the filestyle plugin, I can clear the field. Thanks.
you should have mentioned about that in your question, you could have got a more accurate answer
Thanks to you I figured it out. Set the .file class value to "" AND disable the file input id adfile" $('#addaduploadform .file').val(''); $('#adfile').attr('disabled', 'disabled');
1
$(":file").filestyle('clear');

Comments

0
<html>
<head>

<script type="text/javascript">
$(document).ready(function(){
    $('#adtype').change(function(){
        if($('#adtype option:selected').val()==="7"){
            $('#adfile').remove();
            $('#file_div').html('<input type="file" id="adfile" value="" />')
            $("#adfile").prop('disabled', true);
        }else{
            $("#adfile").prop('disabled', false);
        }
    });
});

</script>
</head>

<body>
    <div id="signup">
        <form id="someform" action="somepage" method="post">
       <select name="selectstuff" id="adtype">
<option value="1">one</option>
<option value="5">five</option>
<option value="7">seven</option>
</select>
        <div id="file_div">
         <input type="file" id="adfile" value="" />

        </div>

        <input type="submit" value="submit" />
        </form>

    </div>

</body>

</html>

2 Comments

I tried that technique yesterday. Won't work. I'll just chalk this up to the filestyle plugin interfering with the other JQuery. Thanks for the effort
i have checked it in ie and all other browser.it works fine just add jquery.js in this code and test.
0

This worked for me.

$("#Elementid").val(null);

Comments

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.