Solved

Enable and disable radio and textfield depending on checked checkbox

Posted on 2006-07-18
5
225 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 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

680 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