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>






It seems you are missing the function keyword if you are not using typescript or es6

– brk
Sep 16 '18 at 14:42


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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)