I have a <div> in my page like this:
<div class="errordiv" style="display: none;">Username is empty</div>
I have an input field and a button like this:
<input type="textbox" id="txt1" />
<input type="button" value="Submit" id="btn1" onclick="validate();" />
I have tried this,
function validate() {
//alert('hi');
var inptextbox = document.getElementById('txt1');
//alert('level zero');
var errorMsg = document.getElementsByClassName('errordiv').item();
//alert('level ground');
if (inptextbox.value == '') {
//alert('hi2');
errorMsg.style.display = 'block';
} else {
errorMsg.style.display = 'none';
}
}
The above js code is not working. I am not getting the level ground alert. I don't know what is missing.
P.S : I don't want Jquery as my page has some restriction to use library files.
var errorMsg = document.getElementsByClassName('errordiv').item();replace.item()by[0]:var errorMsg = document.getElementsByClassName('errordiv')[0];.item()is fine[0]more readable and less error-prone, but if it works as it is, alright..item()expects an index argument and is throwing aNS_ERROR_XPC_NOT_ENOUGH_ARGS: Not enough argumentserror on Firebug when called without passing an argument. jsfiddle.net/ult_combo/5M6Tu