Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enable and disable radio and textfield depending on checked checkbox

Posted on 2006-07-18
5
Medium Priority
?
233 Views
Last Modified: 2010-08-05
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
Comment
Question by:jenmat
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 17131705
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
 
LVL 19

Expert Comment

by:nschafer
ID: 17132073
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
 

Author Comment

by:jenmat
ID: 17136609
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
 
LVL 19

Accepted Solution

by:
nschafer earned 1000 total points
ID: 17137718
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
 

Author Comment

by:jenmat
ID: 17137734
It did, great, thanks!
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question