How to handle ‘undefined’ in javascript
Variable ‘mydata’ is returning the value ‘undefined’, if you give this in an if condition its not working.. if(mydata==”undefined”) this condition should not work.so below code will solve the issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <script> function test_type_of() { var mydata=document.getElementById('myhidden').val; if (typeof mydata == 'undefined') { //if(mydata=="undefined") //{ alert('test'); } } </script> </head> <body> <input type="hidden" value="" name="myhidden" id="myhidden"> <input type="button" name="click_button" id="click_button" value="Click" onclick="test_type_of();"> </body> </html> |