Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to call a java script function from a html check box as follows
XML
<script type="text/javascript">
changeGroup=function(val){
                var check=val.checked;
                document.getElementById("toGroup").disabled=!check;
            }
</script>

<input type="checkbox" id="changeGroup" onchange="changeGroup(this);"/>


But it does not working. Can any one give me, what the problem in this code.
Posted
v2
Comments
Shubham Choudhary 21-Mar-13 6:35am    
hi!!! agav try to onclick instead of onchange

Hey,

It is working fine.

I don't know the control you have taken for "toGroup".
But I have taken that as a button and tested, the code is working.

Case-1
When checkbox is checked, the below line will calculate as true.
JavaScript
var check = val.checked;

So, the button will be enabled as per the code below.
JavaScript
document.getElementById("toGroup").disabled = !check; //As !check is false


Case-2
When checkbox is un-checked, the below line will calculate as false.
JavaScript
var check = val.checked;

So, the button will be disabled as per the code below.
JavaScript
document.getElementById("toGroup").disabled = !check; //As !check is true


Check the Live Demo[^].

Thanks...
 
Share this answer
 
Comments
gavkk 21-Mar-13 10:07am    
But still the problem is there.

toGroup is a select associated with bean
<html:select styleId="toGroup" property="toGroup">

The similar code working good in another jsp file.
I could not get you.
Is it a dropdown ?
the problem solved by changing function name. The function name and id of checkbox cannot be same!
 
Share this answer
 
Comments
But it was working with me. You can see the demo I have provided in my answer.
hi friend toGroup is visible false then following try to
JavaScript
<script type="text/javascript">
function(val){
                var check=val.checked;
                document.getElementById("toGroup").disabled=!check;
            }
}</script>
 
Share this answer
 
v2
Comments
sainadh.chintha 21-Mar-13 7:11am    
with out function name what it mean. His actual code is working fine.
try these,

XML
function changeGroup(id){
var check=id.checked;
document.getElementById("toGroup").disabled=!check;

}


<input type="checkbox" id="changeGroup" onchange="changeGroup(this.id);"/>
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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