Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML
I try to call a java script function from a html check box as follows
<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 21-Mar-13 0:52am
gavkk1.6K
v2
Comments
Shubham Choudhary at 21-Mar-13 6:35am
   
hi!!! agav try to onclick instead of onchange
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
var check = val.checked;
So, the button will be enabled as per the code below.
document.getElementById("toGroup").disabled = !check; //As !check is false
 
Case-2
When checkbox is un-checked, the below line will calculate as false.
var check = val.checked;
So, the button will be disabled as per the code below.
document.getElementById("toGroup").disabled = !check; //As !check is true
 
Check the Live Demo[^].
 
Thanks...
Comments
agav at 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.
Tadit Dash at 22-Mar-13 2:21am
   
I could not get you.
Is it a dropdown ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

the problem solved by changing function name. The function name and id of checkbox cannot be same!
  Permalink  
Comments
Tadit Dash at 22-Mar-13 3:08am
   
But it was working with me. You can see the demo I have provided in my answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi friend toGroup is visible false then following try to
<script type="text/javascript">
function(val){
                var check=val.checked;
                document.getElementById("toGroup").disabled=!check;
            }
}</script>
  Permalink  
v2
Comments
sainadhchintha at 21-Mar-13 7:11am
   
with out function name what it mean. His actual code is working fine.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

try these,
 
function changeGroup(id){
var check=id.checked;
document.getElementById("toGroup").disabled=!check;
 
}
 

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

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100