+ 1
adding javascript to my question
https://code.sololearn.com/WdB9Z70JD78l/?ref=app so guys, now i want to alert "right" if cristiano is chosen and "wrong" if messi or neymar... just for example
1 Odpowiedź
+ 3
The following JavaScript does as you describe:
document.addEventListener('DOMContentLoaded', function() {
var playerSelect = document.querySelector('[name="player"]');
var submitButton = document.querySelector('button');
submitButton.addEventListener('click', function() {
var selectedPlayerName = playerSelect.value;
var msg;
if (selectedPlayerName === 'cristiano')
msg = 'right';
else
msg = 'wrong';
alert(msg);
});
});
The datalist tags in your select should be removed because putting them there isn't valid HTML5 markup. Also, you forgot to add cristiano as an option. Cristiano was mentioned in your question but not in the HTML.
This is the corrected HTML:
<select name="player">
<option value="cristiano">Cristiano</option>
<option value="neymar">Neymar</option>
<option value="messi">Messi</option>
<option value="ronaldo">Ronaldo</option>
</select>
This is minor but I would put a descriptive id on your submit button and query based on that so the JavaScript is more likely to select the intended button if/when you add more buttons to the HTML. I didn't include that above in case that change would confuse you.