I checked the following code in JSLint:
DEMOCode:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Reset textarea</title> </head> <body> <textarea id="ta"> <p>Hello</p> </textarea> <input type="button" value="Reset" onclick="reset();"> <script> function reset() { var ta = document.getElementById('ta'); if (!ta.value || ta.value != ta.defaultValue && confirm('Are you sure?')) { ta.value = ta.defaultValue; } } </script> </body> </html>
And I got the following errors:
Since the textarea value and default value are always of the type string, then there's no type conversion before comparison and it makes absolutely no difference except making your JS file larger!Expected '!==' and instead saw '!='.
Why should I add parenthesis when && has a higher precedence than ||? I can simply remove nested parenthesis instead of:The '&&' subexpression should be wrapped in parens.
Please correct me if I'm mistaken.Code:if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))



Reply With Quote


Bookmarks