Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Checkbox Toggle making me go grey...

Posted on 2011-09-06
9
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

705 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