tags:

views:

35

answers:

1
<form action="" method="post" name="site_avatar" id="avatar" enctype="multipart/form-data">
<input type="radio" name="avatar" value="gravatar" />
<input type="radio" name="avatar" value="local" />
<input size="25" name="file" type="file" />
<input name="site_avatar" type="submit" value="Submit" />
</form>

This is my form with two radio buttons and one image upload field. I want to keep the "Browse" button disabled if the second radion button isn't selected (with the value "local").

Can anyone please help?

+2  A: 

[See it in action]

Javascript

var form = document.getElementById("avatar");
var second = form.getElementsByTagName("input")[1];
var file = document.getElementById("file");

form.onchange = function() {
  if (second.checked) {
    file.disabled = false;
  } else {
    file.disabled = "disabled";
  }
};

HTML

<input size="25" name="file" id="file" type="file" />

I would suggest you revisiting the names & ids in the form btw, because they are kinda messed up. :)

galambalazs
Works fine but there is a little problem. If the second radio button is selected by default, i.e. checked="checked" tag is added, the button is still disabled... So user has to check the first button and that check the second again to activate the "Browse" button...
Levani
1.) be the first one the default 2.) if the second one is the default, make the browse enabled by default (both works...)
galambalazs