Solved

Enable and disable radio and textfield depending on checked checkbox

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now