Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this click function for a checkbox. The checkbox starts out unclicked. If I click it, this is not true

$chkbox.is(":checked")


Only for the first time I click the checkbox, then after that I can check and uncheck the box and everything works. But the first click doesn't work. Anyone know why?


JavaScript
$("#oneTerminalCheckbox").click(function(){
    displayTerminals($(this), testarry)
});



JavaScript
function displayTerminals($chkbox, arry)
{
	$chkbox.click(function(){
		if($chkbox.is(":checked"))
		{
			ToggleCircleArray(arry);
		}
		else
		{
			ToggleCircleArrayRemove(arry);
		}
	});
}


What I have tried:

I put an alert inside the is checked if statement, but it doesn't get into it for the first click.
Posted
Updated 26-Oct-22 5:28am
v2

1 solution

Try using the .change function instead

JavaScript
$('#oneTerminalCheckbox :checkbox').change(function() {
    if (this.checked) {
        // the checkbox is now checked 
    } else {
        // the checkbox is now no longer checked
    }
});
 
Share this answer
 
Comments
Member 8428760 26-Oct-22 11:35am    
Thank you Mike. I had the click function inside the method again. lol
Mike Hankey 26-Oct-22 11:52am    
You're welcome.

Ah yes, I hadn't noticed that.

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