currently a beginner learning javascript but I'm a bit stuck on how to add in this code if the inputs are empty, to shoot an alert. Would I add another function within the first? Any hints would be appreciated!
function getCandidateNames() {
var inputs = document.getElementsByTagName("input");
var result = [ ];
for ( var i = 0; i < inputs.length ; i += 1) {
if ( inputs[i].getElementsByTagName("candidate") ) {
result.push( inputs[i].value );
}
}
return result;
}
function putCandidateNames(names) {
document.getElementById("candidateName1").innerHTML = names[0];
document.getElementById("candidateName2").innerHTML = names[1];
document.getElementById("candidateName3").innerHTML = names[2];
}
function candidateNames() {
putCandidateNames ( getCandidateNames() ) ;
}
The HTML
<fieldset id="candidates">
<legend>Candidates</legend>
<div>
<label for="cand1">Candidate 1</label>
<input class="candidate" id="cand1" placeholder="Candidate">
</div>
<div>
<label for="cand2">Candidate 2</label>
<input class="candidate" id="candName" placeholder="Candidate">
</div>
<div>
<label for="cand3">Candidate 3</label>
<input class="candidate" id="candName" placeholder="Candidate">
</div>
</fieldset>
Aucun commentaire:
Enregistrer un commentaire