Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone......

I need help to disable textbox in onclick checkbox event...
Here is my code:
XML
function ascheckpossible() {
            //debugger;
            var aspossible = document.getElementById("<%=aspossible.ClientID%>");

            if (aspossible.checked == true) {
                document.getElementById("time").style.visibility = "hidden";
                document.getElementById("datetime").style.visibility = "hidden";
                document.getElementById("timedate").style.visibility = "hidden";

            }
            else {
                document.getElementById("time").style.visibility = "visible";
                document.getElementById("datetime").style.visibility = "visible";
                document.getElementById("timedate").style.visibility = "visible";
                document.getElementById("<%=deliveryTime.ClientID %>").value = "";
                document.getElementById("<%=hour.ClientID %>").value = "";
                document.getElementById("<%=min.ClientID %>").value = "";
                document.getElementById("<%=ddltime.ClientID %>").value = "AM";

            }
        }

Here aspossible is checkbox id and time,timedate,datetime is id of textbox inside div tag.

Pls help me...
Posted

1 solution

Just call this function on a handler of the event onclick of the check box. Please see:
http://www.w3schools.com/jsref/dom_obj_event.asp[^].

JavaScript
<input type="checkbox" id="<%=aspossible.ClientID%>" onclick="ascheckpossible()" />


Also, call this function to initialize visibility and other setting according to the initial state of your check box.

This is very convenient to do with jQuery. It produces much more compact and easy-to-support code. Please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com/[^],
http://docs.jquery.com/How_jQuery_Works[^],
http://api.jquery.com/category/selectors/[^],
http://api.jquery.com/category/events/[^],
http://api.jquery.com/category/attributes/[^].

Two last links embrace all library features you need to know to solve you simple problem: selectors would replace document.getElementById and return jQuery wrappers to any element, and events would replace event properties and handler calls, using attribute functions. All the code is usually written as a single handler of the $(document).ready event:

JavaScript
$(document).ready(function() {
   myCheckBox = $("#<%=aspossible.ClientID%>");
   // other elements
   myCheckBox.//change any properties
   myCheckBox.click(function() {
      //some code using myCheckBox status and other element properties, such as attributes 
   })
 });


—SA
 
Share this answer
 
Comments
vivekx2 4-Mar-12 3:21am    
pls help me to change in my javascript code
Sergey Alexandrovich Kryukov 4-Mar-12 3:30am    
I don't see any problems if your code. You just need to add a handler and call your function, as I show above.
Where is your checkbox element in the code? You can also modify it just to add the "onclick".

Or did I convince you to refactor it to use jQuery?
My code using jQuery is just the skeleton. You would need some time to complete it, but it's easy enough.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900