tags:

views:

97

answers:

6

I have the following drop down:

<select name="chooseLanguage">
    <option>Choose One</option>
    <option value="java">Java</option>
    <option value="php">PHP</option>
    <option value="c++">C++</option>
</select>

If ANYTHING is selected from this dropdown other than the default "Choose One" which is of course preselected, then I want this input field to disappear:

<input type="file" name="uploadFile" />

How can this be achived using jQuery (if possible with a nice fade effect)?

EDIT

My entire code, for some reason none of the answers have worked for me.

<script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>

<script type="text/javascript">
$(function(){
  $("#chooseLanguage").change(function(){
    if ($(this).val() == '') {
      $("#uploadFile").hide();
    } else {
      $("#uploadFile").show();
    }
  });
});
</script>

<form action="" method="post">

<select name="chooseLanguage" id="chooseLanguage">
  <option>Choose One</option>
  <option value="java">Java</option>
  <option value="php">PHP</option>
  <option value="c++">C++</option>
</select>

<input type="file" name="uploadFile" id="uploadFile" />

</form>
+4  A: 
<input id="uploadFile" type="file" name="uploadFile" />

then you can just use $('#uploadFile').hide()

as suggested you have also fadeOut() and fadeIn() for the opposite effect..

Jack
+2  A: 

Give the input an id, like id="myupload".

Then, simply:

$('#myupload').fadeOut('slow');
David Titarenco
+1  A: 

You can do like:

$('select[name="chooseLanguage"]').change(function(){
  if ($(this).val())
  {
     $('input[name="uploadFile"]').fadeOut('slow');
  }
});
Sarfraz
+1 - Except that checking for `$(this).val()` won't do much good because if there's no `value` set, it returns the text contained in the option (for some browsers anyway).
patrick dw
A: 

Here's another way to do it. Example: find all <div> elements that have a CSS class of "product", and animate them to slowly disappear:

$("div.product").slideUp('slow').addClass("removed");
DOK
+1  A: 

jQuery

$(document).ready(function(){
  $("#chooselanguage").change(function(){
    if ($("#chooselanguage").attr("selectedIndex")>0) {
      $("#fileupload").fadeOut();
    } else {
      $("#fileupload").fadeIn();
    }
  });
});

HTML

<select id="chooselanguage" name="chooseLanguage">
  <option>Choose One</option>
  <option value="java">Java</option>
  <option value="php">PHP</option>
  <option value="c++">C++</option>
</select>
Gert G
A: 

The other answers are nearly correct, but are missing a minor detail or two. This should hopefully work "out of the box".

$(function(){
  $("#chooseLanguage").change(function(){
    if ($(this).val() == '') {
      $("#uploadFile").hide();
    } else {
      $("#uploadFile").show();
    }
  });
});

Just give the "chooseLanguage" select box an ID of "chooseLanguage", and the upload input an ID of "uploadFile".

Wireblue
I haven't chosen any correct answers yet because none of the answers have worked for me, including yours. Please see my edit in the question, I have posted my entire code. Can you please run it and see why it isn't working?
Ron
@Ron: See my note at the top. My solution works.
Gert G
Apologies Ron. Because the "Choose One" option was missing a blank value attribute, the IF statement didn't work correctly. Up-voted your answer Gert G.
Wireblue
Thanks Wireblue. Much appreciated. :)
Gert G