Click here to Skip to main content
11,576,293 members (61,089 online)
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 20-Mar-13 23:52pm
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 Sergey Alexandrovich Kryukov 285
1 F-ES Sitecore 140
2 Abhinav S 115
3 Lucifier Rocks 105
4 OriginalGriff 105
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 630
2 Abhinav S 438
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 Mar 2013
Copyright © CodeProject, 1999-2015
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