Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Check Text Field Value</title>
<style>
</style>
</head>
<body>
<textarea rows="5" cols="35" id="field" oninput="indicate();"></textarea>
<div id="indicator"></div>
<script>
function indicate() {
var field = document.getElementById('field');
var indicator = document.getElementById('indicator');
if (field.value) {
indicator.innerHTML = 'Some content';
} else {
indicator.innerHTML = 'No content';
}
}
indicate();
</script>
</body>
</html>
does this help...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Check Text Field Value</title>
<script>
(function() {
'use strict';
var field,indicator;
function init() {
field=document.getElementById('field');
indicator=document.getElementById('indicator');
field.oninput=function() {
indicate();
}
indicate();
}
function indicate() {
if(field.value.replace(/\s/g,'').length==0) {
indicator.innerHTML='No content';
}
else {
indicator.innerHTML='Some content';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
})();
</script>
</head>
<body>
<textarea id="field" rows="5" cols="35"></textarea>
<div id="indicator"></div>
</body>
</html>
There's no right or wrong answer.
has variable abc been defined?
var abc; // declared but not defined
if (typeof(abc) != 'undefined') {
// it's safe to do something with abc
}
else {
// oops, someone forgot to initialize abc
}
does variable abc have a non-zero (non-"false" etc) value?
// check that abc is non-zero
if (abc != 0) {}
// check that abc is truthy
if (abc) {}
Suppose you want to check whether a variable has been defined, but its value might happen to be zero. Can it be done?
is there a difference between
== false
and
!= true
if (typeof(abc) != 'undefined')
There's no functional difference.