Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Enable and disable radio and textfield depending on checked checkbox

Hi,
i've tried some solutions found here but no one that do exactly what i want.
Take a look here: http://www.destinationspass.se/dev.asp

The field for uploading an image is by default disabled and radiobuttons above to choose an image is enabled. If a user checks the checkbox i want the upload field to be enabled and the radiobuttons to be unchecked and disabled. When user uncheck the checkbox the field for upload should be disabled and radiobuttons enabled again.

Suggestions?
0
jenmat
Asked:
jenmat
  • 2
  • 2
1 Solution
 
enachemcCommented:
use onClick event of the checkbox to trigger a javascript function. In the javascript function enable or disable the required objects depending on the selection state. You can get a refence to the required objects by using the document.getElementById(<id>) (they all should hava an id).
0
 
nschaferCommented:
Hi jenmat,

  Basically this code just shows you how to do what enachemc explained, except that I'm not using getElementByID.

Here is the javascript function that will enable/disable the elements as required:

function setUpload(obj) {
  if (obj.checked) {
    for (i=0;i<document.forms[0]['GiftcardImageID'].length;i++) document.forms[0]['GiftcardImageID'](i).disabled=true;
    document.forms[0]['GiftcardImage'].disabled = false;
  }
  else {
    for (i=0;i<document.forms[0]['GiftcardImageID'].length;i++) document.forms[0]['GiftcardImageID'](i).disabled=false;
    document.forms[0]['GiftcardImage'].disabled = true;
  }  
}


Here is the code for the checkbox:
  <input type="checkbox" name="GiftcardImageUpload" value="0" onclick="setUpload(this);">



Hope this helps,
Neal.
0
 
jenmatAuthor Commented:
Works fine nschafer!
Beacause the number of images shown from db there might be sometime there is only one image (radio button GiftcardImageID) that shall be disabled or enabled. It then fails. Is there a solution for that?
Best / jenmat
0
 
nschaferCommented:
This change should solve that problem.

function setUpload(obj) {
  if (obj.checked) {
    var radio = document.getElementsByName('GiftcardImageID');
    for (i=0;i<radio.length;i++) radio(i).disabled=true;
    document.forms[0]['GiftcardImage'].disabled = false;
  }
  else {
    var radio = document.getElementsByName('GiftcardImageID');
    for (i=0;i<radio.length;i++) radio(i).disabled=false;
    document.forms[0]['GiftcardImage'].disabled = true;
  }  
}
0
 
jenmatAuthor Commented:
It did, great, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now