Duck
Duck

Reputation: 35933

Javascript - trying to click on LI elements using a script

I have a UL with several clicable LI elements. I am doing a Javascript to click on those LI elements automatically. This is what I have so far:

function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName('*'),
            elsLen = els.length,
            pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)'), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

// ajaxListLI is the class of the LI elements I want
var elements = getElementsByClassName(document, 'ajaxListLI');

if I do alert(elements.length) at this point, I get the correct number of LI on the page

but if I try to click on one of the elements, using for example

elements[2].click();  //2 is just an example. It can be any number

or I try to hide the element using

elements[2].style.display = 'none';

absolute nothing happens... what am I missing?

thanks.

Upvotes: 0

Views: 502

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Works fine for me. Live demo.

Upvotes: 2

Related Questions