I need to develop an html code in which several checkboxes and a button appear and with a script that when the button is pressed, a message appears with those that have been pressed. This should be the result:
However, the code that I have made does not give an answer. I attach the code:
<html>
<script language="JavaScript">
function calculs(){
var football = "";
var basket = "";
var swimming = "";
if(document.getElementById("checkbox1").checked{ football = "Football"; }
if(document.getElementById("checkbox2").checked{ basket = "Basket"; }
if(document.getElementById("checkbox3").checked{ football = "Swimming"; }
alert("The selected sports are" + Football + " " + Basket + " " + Swimming);
}
</script>
<h1>Cube exercise</h1>
<form>
<p><input type="checkbox" id="password">Football</p>
<p><input type="checkbox" id="password">Basket</p>
<p><input type="checkbox" id="password">Swimming</p>
<input type="button" value="Send" onClick="calculs()" > <br>
</form>
</html>
click
of this same element so that it is through this that we identify which elements activated the eventclick
evento.target.value
click
form button event we print the newly filled array with the elements chosen by the userCode:
EDITION
Now, as I indicated that I am going to obtain the text of the attribute
value
when clicking on the elements within it,div
if by mistake I click on a space without inputs within it, I will generate a value to be obtained, that isundefined
why I did the validation with the if/else to prevent those values from entering the array.On the other hand, the events will be fired within the web page when the user performs an action on one or several elements of your web, for example when the user clicks on the checkboxes it is an event and when you press the form button it is another event. .
For the last point I am interested in the
click
button event because when the user presses it I want the chosen values to be displayed; then I make use of a listener that is a listener of these events which, with the syntax shown, requests: event and function with the actions to be carried out when the user causes said event .REFERENCES