javascript - JS: Do different things depending on if value is null -
<!doctype html> <html> <head> <meta charset="utf-8"> <title>document</title> <script> function mygender() { if (document.queryselector('input[name="gender"]:checked').value==null) { document.getelementbyid('demo').innerhtml = "please select gender"; } else { document.getelementbyid('demo').innerhtml = document.queryselector('input[name="gender"]:checked').value; } } </script> </head> <body> <label for="gender"> gender: </label> <br> <input type="radio" name="gender" value="man"> man<br> <input type="radio" name="gender" value="woman" >woman<br> <input type="radio" name="gender" value="other" >other<br> <button onclick="mygender()">what i?</button> <p id="demo"></p> </body> </html>
https://jsfiddle.net/vykptm5x/
i want if have no gender selected, , dont want make checked default. still says in console null, have there if statement.
thank :)
you want check if
document.queryselector('input[name="gender"]:checked')
is null
, not if value null. trying access value cause error since value
property on null object.
simply use console debugger , see yourself
Comments
Post a Comment