Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2008, 02:05 PM
Authorized User
Points: 254, Level: 5
Points: 254, Level: 5 Points: 254, Level: 5 Points: 254, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2007
Location: , , .
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using JS to enable/disable??

Need some help because I can't find an answer using google.

I need to be able to enable/disable a select field in a form, but when disabled, I want the value set to null and the drop down grayed out.

Below is a short what I have but it doesn't work. Need a little help please..


Code:
    <script language="JavaScript">
        function ckbox(y){
            with(document.form_product){
                if(y=="1"){                    
                    document.form_prodcode.value=''; document.form_prodcode.disabled=true;
                }else if(y=="0"){
                    document.form_prodcode.disabled=false;                
                }
            }
        }
    </script>

    <form>

        INCLUDE PRODUCT INFO:
            <input type="radio" name="form_product" value = "0" onclick="ckbox(this.value);" checked>YES&nbsp;&nbsp;
            <input type="radio" name="form_product" value = "1" onclick="ckbox(this.value);">NO

        PRODUCT CODE 
            <select name="form_prodcode">
                <option value=""></option>
                <option value="A321">A321</option>
                <option value="A535">A535</option>
                <option value="B765">B765</option>
                <option value="C124">C124</option>
            </select>

    </form>
Thanks!!
 
Old November 10th, 2008, 02:46 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. What is it not doing? What is the purpose of your with statement?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old November 10th, 2008, 03:33 PM
Authorized User
Points: 254, Level: 5
Points: 254, Level: 5 Points: 254, Level: 5 Points: 254, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2007
Location: , , .
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's not doing anything. When I select Yes or No in the Product field, I need the Product Code drop down to either Enable itself or Disable itself. The way the code is now, it doesn't do a thing, so I need some help to fix it.


 
Old November 10th, 2008, 05:04 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Your problem is that you can not use with() in this context (in reality it should of thrown you an error) as it is only used to set the properites of an object. For example:

Code:
with(someElement.style)
{
color = "black";
}
The following JavaScript will suit your needs:

Code:
    <script language="JavaScript">
        function ckbox(y){
            //with(document.form_product){
                if(y=="1"){                    
                    document.getElementById('form_prodcode').value=''; document.getElementById('form_prodcode').disabled=true;
                }else if(y=="0"){
                    document.getElementById('form_prodcode').disabled=false;                
                }
            //}
        }
    </script>
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
 
Old November 10th, 2008, 06:48 PM
Authorized User
Points: 254, Level: 5
Points: 254, Level: 5 Points: 254, Level: 5 Points: 254, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2007
Location: , , .
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!! Worked like a charm!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enable / Disable Form Fields Automatically jackyam Javascript How-To 10 March 17th, 2008 04:46 AM
Log4j - Enable/Disable logging programmatically albinjoseph Pro Java 7 July 26th, 2006 05:15 PM
Disable and Enable at runtime Hammam ASP.NET 1.0 and 1.1 Professional 0 May 17th, 2006 12:40 AM
Disable/Enable elements ldoodle Javascript How-To 3 June 7th, 2005 05:48 AM
Enable & Disable a field using Javascript laxmig Javascript How-To 6 January 24th, 2005 08:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.