Solved

Enable and disable radio and textfield depending on checked checkbox

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to deal with the query string 8 37
Make Session back to empty 9 30
Google Analytics setup 5 50
Button function on table is in trouble 3 20
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 …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

856 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