0

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(){
//get Checkboxes
var fitnessCheckbox = document.getElementById("fitnessCheck");
var beautyCheckbox = document.getElementById("beautyCheck");
var streetwearCheckbox = document.getElementById("streetwearCheck");
var luxusCheckbox = document.getElementById("luxusCheck");
var datenschutzCheckbox = document.getElementById("datenschutzCheck");

if(fitnessCheckbox.checked == true || beautyCheckbox.checked == true || streetwearCheckbox.checked == true || luxusCheckbox.checked == true){
    datenschutzCheckbox.style.display ="block"; 
}
}</script>
2
  • 2
    It seems you are missing the function keyword if you are not using typescript or es6 Commented Sep 16, 2018 at 14:42
  • Yes, the function keyword is missing. Commented Sep 16, 2018 at 14:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.