简体   繁体   中英

How do you call a function with parameters using onclick?

I have a list of checkboxes that I want to display based on current time when user clicks a button.

JS:

var hour = (new Date()).getHours();
showMed('A', hour == 19);
showMed('B', hour == 19);
showMed('C', hour == 13);

function showMed(med, show) {
document.getElementById('med' + med).style.display = show ? '' : 'none';
}

HTML

<div class="inner" id=text><button onClick="showMed()">Check</button></div>
<form>
<div id='medA'>
<input type="checkbox" name="Med A" value="A">Medication A
</div>
<div id='medB'>
<input type="checkbox" name="Med B" value="B">Medication B
</div>
<div id='medC'>
<input type="checkbox" name="Med C" value="C">Medication C
</div>
</form>

I want the user to click the button and it will display the checkboxes based on the current time. But at the moment I can't seem to call the function

I guess you should try this:

JS:

function myFunction(){
   var hour = (new Date()).getHours();
   showMed('A', hour == 19);
   showMed('B', hour == 19);
   showMed('C', hour == 13);
}

function showMed(med, show) {
   document.getElementById('med' + med).style.display = show ? '' : 'none';
}

HTML

 <div class="inner" id=text><button onClick="myFunction()">Check</button></div>
<form>
<div id='medA'>
<input type="checkbox" name="Med A" value="A">Medication A
</div>
<div id='medB'>
<input type="checkbox" name="Med B" value="B">Medication B
</div>
<div id='medC'>
<input type="checkbox" name="Med C" value="C">Medication C
</div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM