Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It two checbox, it is work when check it is dysplay my div , but if uncheck it doesn't. I don't know how i fix this...


I have my code like taht



<table>

<pre lang="xml"><tr>
     <td>
           Run</td>
            <td>
                <input type="checkbox" ID="chkrun"   runat="server" class="check"  value="Y"  onclick="OptionTwo(this)" />
            </td>
            </tr>

      <tr>
     <td >
           walk</td>
            <td>
                <input type="checkbox" ID="chkwalk"   runat="server" class="check"  value="Y" onclick="OptionOne(this)"/>
            </td>
            </tr>










<pre lang="msil">function selectOptionTwo(switchElement){

    if (switchElement.value == 'Y') {
         document.getElementById("SchedulePickup").style.display = "none";
    }

{
        document.getElementById("SchedulePickup").style.display = 'block';
    }
}

  <script type="text/javascript">

 function OptionOne(Element){

    if (switchElement.value == 'Y') {
         document.getElementById("div").style.display = "block";
    }
     else
{
        document.getElementById("div").style.display = "none";
    }

}

function OptionTwo(Element){

    if (switchElement.value == 'Y') {
         document.getElementById("div").style.display = "none";
    }

}




    </script>


Posted

You check if the value is Y, but you never change it. Therefore, you're checking something arbitrary that means your code always does the same thing.
 
Share this answer
 
Comments
Philippe Mori 8-Jul-11 19:20pm    
I think that an else is also missing in selectOptionTwo function.
Chamiss 9-Jul-11 1:08am    
Thanks cristian...i tried ready...it was not working
You Function OptionOne and OptionTwo pass "Element" for CheckBox object
But you are Checking "switchElement".

JavaScript
function OptionOne(Element){
    if (Element.value == 'Y') {
         document.getElementById("div").style.display = "block";
    }else {
        document.getElementById("div").style.display = "none";
    }

}


Secondly you can use CheckBox's checked property rather than value property.
it would automatically as you check and uncheck , else you have to set value Y and N as you check as Chris mentioned.

Hope this helps.

-***********************
Comment 2 -
Hi
I would just say/suggest please copy paste this code to a html file and run this on any browser
and then start over to think what you need...do the testing on this html page..

XML
<html>
<body>
<script type = "text/javascript">
function CheckOne(ele) {
   if (ele.checked) {
       document.getElementById("div1").style.display = 'none';
   }else {
       document.getElementById("div1").style.display = 'block';
   }
}
</script>

<div id = "div1">
<input type = "text"> Hello </input>
</div>
<input type = "checkbox" id = "checkbox1" onClick="CheckOne(this);"/>
</body>
</html>


Thanks
 
Share this answer
 
v4
Comments
Chamiss 9-Jul-11 1:07am    
Yes i fix , but still give same thing... i have to check one of them either fisrt or second one.. both pass to database 'Y' VALUE...it is ready insert and everything...but I only need when chkwalk is check display the block otherwise it is uncheck hidden...thank you...
harish85 9-Jul-11 1:45am    
Updated Check Comment 2 Section in solution above.
Update on above html what you need, you can find the required behavior yourself, I am not following your requirement.

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