Solved

Javascript Checkbox Toggle making me go grey...

Posted on 2011-09-06
9
310 Views
Last Modified: 2012-05-12
Hello,

I've been strugling with this now for hours, I just can't get it to work; what I have is -

A checkbox
A textfield

Rule should be -

When checkbox checked or checkbox.value = Y
Textfield Disabled

When checkbox NOT checked or checkbox.value NOT Y
Textfield Enabled

Should be able to toggle between the two by checking / unchecking...

My checkbox textfield combo pair are -

<tr>
        <td class="fixedleftwidth">Carriage Included (Y/N)</td>
        <td><input name="procrriageinc" type="checkbox" disabled="disabled" id="procrriageinc" onclick="Disable_Pro_Special_Carriage(this.checked)" value="Y"/></td>
      </tr>
      <tr class="alternativerowtr">
        <td class="fixedleftwidth">Carriage Rate (If Not Included)</td>
        <td><input name="procarriagerate" type="text" id="procarriagerate" value="0.00" /></td>
      </tr>

Open in new window


My javascript contains over rules, ALL listed in bottom code segment, but the ones in particular affecting this are -

function Disable_Pro_Special_Carriage(status)
{
if(document.getElementById('procarriagerate').disabled  = 'true')
document.getElementById('procarriagerate').disabled  = 'false';
else
document.getElementById('procarriagerate').disabled  = 'true';
}

Open in new window


Currently what happens is,

First time checkbox is checked, the textfield is disabled, subsequently nothing happens for each additional click....

HELP

TY
window.onload = function resettoggle() {
var e = document.getElementById('ULSpecialDescriptionTGI');
e.style.display = 'none';
var e = document.getElementById('ULSpecialDescriptionPro');
e.style.display = 'none';
var e = document.getElementById('Pro-Direct-Special-Header');
e.style.display = 'none';
var e = document.getElementById('TGI-Direct-Special-Header');
e.style.display = 'none';

var e = document.getElementById('TGI-Direct-Pricing-Wrapper');
e.style.display = 'none';
var e = document.getElementById('TGI-Direct-Carriage-Wrapper');
e.style.display = 'none';
var e = document.getElementById('PRO-Direct-Pricing-Wrapper');
e.style.display = 'none';
var e = document.getElementById('PRO-Direct-Carriage-Wrapper');
e.style.display = 'none';
}

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}

function Disable_Pro_Special_Carriage(status)
{
if(document.getElementById('procarriagerate').disabled  = 'true')
document.getElementById('procarriagerate').disabled  = 'false';
else
document.getElementById('procarriagerate').disabled  = 'true';
}
function Disable_TGI_Special_Carriage()
{
if(document.getElementById('tgicarriagerate').disabled  = 'true')
document.getElementById('tgicarriagerate').disabled  = 'false';
else
document.getElementById('tgicarriagerate').disabled  = 'true';
}

function enable_text(status)
{
status=!status; 
document.getElementById('normalproprice').disabled = status;
document.getElementById('specialpro').disabled = status;
document.getElementById('procrriageinc').disabled = status;
}

function enable_TGI_text(status)
{
status=!status; 
document.getElementById('normaltgiprice').disabled = status;
document.getElementById('specialtgi').disabled = status;
document.getElementById('tgicrriageinc').disabled = status;
}


function enable_TGI_Special_Offer(status)
{
status=!status; 
document.getElementById('displayspecialpricetgi').disabled = status;
document.getElementById('displayspecialdesctgi').disabled = status;

}

function enable_PRO_Special_Offer(status)
{
status=!status; 
document.getElementById('displayspecialpricepro').disabled = status;
document.getElementById('displayspecialdescpro').disabled = status;

}

function enable_TGI_Special_Price(status)
{
status=!status; 
document.getElementById('saletgiprice').disabled = status;
}

function enable_PRO_Special_Price(status)
{
status=!status; 
document.getElementById('saleproprice').disabled = status;
}

Open in new window

0
Comment
Question by:garethtnash
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:stalhw
ID: 36489213
display='block';
can't be used for everything try using this instead:
.display='';
0
 
LVL 8

Expert Comment

by:stalhw
ID: 36489276
but that's not your problem...
I'm curious, since your checkbox is DISABLED, how do you expect people to check/uncheck it ?

I must say I was lazy and didn't go through all your JS code, but something as simple as the following should work:
<tr>
        <td class="fixedleftwidth">Carriage Included (Y/N)</td>
        <td><input name="procrriageinc" type="checkbox" id="procrriageinc" onclick="if (this.checked) { this.form.procarriagerate.disabled=falsel } else { this.form.procarriagerate.disabled=true; }" value="Y"/></td>
      </tr>
      <tr class="alternativerowtr">
        <td class="fixedleftwidth">Carriage Rate (If Not Included)</td>
        <td><input name="procarriagerate" type="text" id="procarriagerate" value="0.00" /></td>
      </tr> 

Open in new window

But I don't undestand why you are saying if 'checked' OR if value='Y'
The value won't change when you check/uncheck so from your own logic, nothing whould happen when you check/uncheck since the value won't change...
Please explain...
0
 

Author Comment

by:garethtnash
ID: 36489369
Thanks stalhw,

two questions, -

Can I include the later script in an external javascript page?

AND

'display='block';
can't be used for everything try using this instead:
.display='';' - Why?

Thank you
0
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

 
LVL 8

Expert Comment

by:stalhw
ID: 36489444
You could but you would have to pass variables to it...

but looking back at your code:
function Disable_Pro_Special_Carriage(status)
{
if(document.getElementById('procarriagerate').disabled  = 'true')
document.getElementById('procarriagerate').disabled  = 'false';
else
document.getElementById('procarriagerate').disabled  = 'true';
}

Why are you passing a variable 'status' (checked true or not) but then you are not using it...
anyway the error in that code is:
  if(document.getElementById('procarriagerate').disabled  = 'true')
it should be ==
  if(document.getElementById('procarriagerate').disabled  == 'true')
or much simpler:
  if(document.getElementById('procarriagerate').disabled)


For the 'block' well it's from experience...
Some html elements don't support display='block' but if it's working for your application, forget what I said.... ( example: can't have a TR display:block, and I'm pretty sure there was others, but can't remember)
0
 

Author Comment

by:garethtnash
ID: 36489462
umm...

Why are you passing a variable 'status' (checked true or not) but then you are not using it...

So how do I use it? or should I remove it?

thanks
0
 
LVL 8

Expert Comment

by:stalhw
ID: 36489516
well it depends...
if your checkbox is 'DISABLED' the status will never change, so you actually don't need to pass this.checked to your function... but it's not a problem, so keep using your code, just replace the = with == in you IFs
0
 

Author Comment

by:garethtnash
ID: 36489684
Hello,

I had that previously, it didn't work :(

Is it best to go with your first suggestion?

Thanks
0
 
LVL 8

Accepted Solution

by:
stalhw earned 500 total points
ID: 36490121
also I dont think you should put true and false between quotes...
try:

function Disable_Pro_Special_Carriage(status)
{
if (document.getElementById('procarriagerate').disabled)
document.getElementById('procarriagerate').disabled  = false;
else
document.getElementById('procarriagerate').disabled  = true;
}

Open in new window

0
 

Author Closing Comment

by:garethtnash
ID: 36501580
Sorry about the delayed response, that has worked beautifully, thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

808 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