0

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.

8
  • It will be good, if some one can show me some working fiddle... Commented Jul 30, 2012 at 8:33
  • var errorMsg = document.getElementsByClassName('errordiv').item(); replace .item() by [0]: var errorMsg = document.getElementsByClassName('errordiv')[0]; Commented Jul 30, 2012 at 8:38
  • @FabrícioMatté .item() is fine Commented Jul 30, 2012 at 8:39
  • I find [0] more readable and less error-prone, but if it works as it is, alright. Commented Jul 30, 2012 at 8:40
  • @Musa Also, are you sure it works? .item() expects an index argument and is throwing a NS_ERROR_XPC_NOT_ENOUGH_ARGS: Not enough arguments error on Firebug when called without passing an argument. jsfiddle.net/ult_combo/5M6Tu Commented Jul 30, 2012 at 8:45

3 Answers 3

1

You need to return false in validate to stop the form from submitting, which reloads the page

EDIT

added polyfill for getElementsByClassName with support for IE7 from https://gist.github.com/2299607

// Add a getElementsByClassName function if the browser doesn't have one
// Limitation: only works with one class name
// Copyright: Eike Send http://eike.se/nd
// License: MIT License
if (!document.getElementsByClassName) {
  document.getElementsByClassName = function(search) {
    var d = document, elements, pattern, i, results = [];
    if (d.querySelectorAll) { // IE8
      return d.querySelectorAll("." + search);
    }
    if (d.evaluate) { // IE6, IE7
      pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
      elements = d.evaluate(pattern, d, null, 0, null);
      while ((i = elements.iterateNext())) {
        results.push(i);
      }
    } else {
      elements = d.getElementsByTagName("*");
      pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
      for (i = 0; i < elements.length; i++) {
        if ( pattern.test(elements[i].className) ) {
          results.push(elements[i]);
        }
      }
    }
    return results;
  }
}

function validate() {
    //alert('hi');
    var inptextbox = document.getElementById('txt1');
    //alert('level zero');
    var errorMsg = document.getElementsByClassName('errordiv')[0];
    //alert('level ground');
    if (inptextbox.value == '') {
        //alert('hi2');
        errorMsg.style.display = 'block';
        return false;
    } else {
        errorMsg.style.display = 'none';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the item syntax correction, that's the only thing that could be crashing it from OP's posted code imo.
@Spencer I added a polyfill for getElementsByClassName with support for IE7
0

Your code works in chrome. This is the demo.

document.getElementsByClassName will not work for some old browsers, check here.

1 Comment

So what is the solution for this? I need to use only getElementByClassName. Why becoz, the errordiv is generating from the server.
0

First off you'll need to return a value from your function to stop the page posting back if a validation even occurs, change your HTML to:

<input type="button" value="Submit" id="btn1" onclick="return validate();" />

Then your script to:

function validate() {
    var inptextbox = document.getElementById('txt1');
    var errorMsg = document.getElementsByClassName('errordiv').item();

    if (inptextbox.value == '') {
        errorMsg.style.display = 'block';
        return false;
    }

    return true;
}

3 Comments

Could do with more info - what version of IE are you using, is it in compatibility mode, what's the actual error message you're getting? Try using an indexer so change your line from document.getElementsByClassName('errordiv').item(); to: document.getElementsByClassName('errordiv')[0];
It is not working in IE7. Also my code is set to document.getElementByClassName('errordiv')[0]; - Any Help?
OK make sure your method name is EXACTLY document.getElementsByClassName('errordiv')[0] (plural), if this still doesn't work try using document.getElementsBySelector('.errordiv')[0] instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.