Javascript function does not work on onclick of checkbox
Javascript function does not work on onclick of checkbox
I try to call a Javascript function through an onclick attribute of a checkbox.
The js should show another checkbox if the first one is checked etc.
Now the function isnt working. I tried to use just an alert() for testing without any result...
Fitness:<input type="checkbox" id="fitnessCheck" onclick="checkFunction()">
Beauty:<input type="checkbox" id="beautyCheck">
Streetwear:<input type="checkbox" id="streetwearCheck">
Luxus:<input type="checkbox" id="luxusCheck" onClick="checkFunction()">
Datenschutz: <input style="display:none" type="checkbox" id="datenschutzCheck">
<script>
checkFunction() beautyCheckbox.checked == true </script>
function
Yes, the function keyword is missing.
– sagars01
Sep 16 '18 at 14:56
2 Answers
2
This is how to define a function in JavaScript.
Correct your function as below;
function checkFunction()
Alternatively, remove the onclick attribute from HTML and put it in the JavaScript as defined below
var fitnessCheckbox = document.getElementById("fitnessCheck");
fitnessCheckbox.onclick() = function() {
// access properties using this keyword
if ( this.checked )
// if checked ...
alert( this.value );
else
// if not checked ...
You have to declare the function checkFunction().
You can do it in many ways such as:
function checkFunction()
// code here
or
var checkFunction = function()
// code here
etc, depending on how you are going to use the function.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
It seems you are missing the
function
keyword if you are not using typescript or es6– brk
Sep 16 '18 at 14:42