+ 1

How to show a message in javascript?

How to show an error if there is no change in option? If no valid option is selected. CODE:- <select name="gender"> <option disabled="disabled" selected="selected">Gender</option> <option>Male</option> <option>Female</option> <option>Other</option> </select>

11th May 2020, 8:04 AM
Faisal Rahman
Faisal Rahman - avatar
5 Réponses
+ 3
You have to use onchange event of select element and a JavaScript function to check which value is selected. Here is basic code for it. HTML: <form name="myForm" onchange="displayValue()"> <select name="gender"> <option>Male</option> <option>Female</option> </select> </form> JavaScript: function displayValue() { console.log(document.myForm.gender.value); }
11th May 2020, 8:13 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Farino i implement your code but problem not fixed yet I'm providing you my code under form tag https://code.sololearn.com/wO5pRk269vie/?ref=app
13th May 2020, 7:50 AM
Faisal Rahman
Faisal Rahman - avatar
0
i want to display the message "No valid option is selected" if default "Gender" option is selected when i click on Submit..
11th May 2020, 8:19 AM
Faisal Rahman
Faisal Rahman - avatar
0
Faisal Rahman I gave you basic code and idea of doing it, try to implement yourself for better practice and let us know if you have any problem trying to implement it 🤓
11th May 2020, 8:23 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
🔫 Rick Grimes Your code will respond on change but i want to respond it even if there is no change in option
11th May 2020, 8:30 AM
Faisal Rahman
Faisal Rahman - avatar