mardi 7 novembre 2017

Trouble passing JS function as parameter

JS Newbie here: I am trying to use JS to append some HTML to a form fieldset when a checkbox is clicked: First, I added the event onchange to the checkbox, and set it to my function:

<fieldset class="lapsFS" id="manLapsFS"><legend>Manual laps</legend>
    <input type="checkbox" name="lapsCB" value="manlapsCB" 
           onchange="cbHandleChange(this, addLap, 'manLapsFS', clearFS, 'manLapsFS');">
    <label>Enable manual laps</label><br>
</fieldset>

cbHandleChange parameters:
1. checkbox to handle
2. function to call when checkbox is on
3. parameter for 2
4. function to call when checkbox is off
5. parameter for 4

here is cbHandleChange:

function cbHandleChange(cb, funcOn, paramOn, funcOff, paramOff) {
if(cb.checked == true) {
    funcOn.call(paramOn);       
else 
    funcOff.call(paramOff);
}

and this is the function passed as parameter 4:

function clearFS(fs) {
    fs.innerHTML = "";
}   

I tried both to put single quotes in onchange="cbHandleChange(this, addLap, 'manLapsFS', clearFS, 'manLapsFS');" rather than to not put them, but still I get a

cbHandleChange is not defined

where am I wrong?

Aucun commentaire:

Enregistrer un commentaire