0

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.

9
  • Do you really need on pure js? Commented Dec 3, 2011 at 11:02
  • why you ask that? I probably do. Commented Dec 3, 2011 at 11:04
  • Seems to be working fine. Have you checked that the return array is having all the desired elements? Commented Dec 3, 2011 at 11:07
  • This is weird, just did a test in jsfiddle, and seems to work: jsfiddle.net/2Xm62 (setting display to 'none') Commented Dec 3, 2011 at 11:08
  • The returned array is not an array, but a collection of LI nodes. How do I print on screen the contents of these nodes, so I can see if I have the right collection? Commented Dec 3, 2011 at 11:12

1 Answer 1

2

Works fine for me. Live demo.

Sign up to request clarification or add additional context in comments.

4 Comments

this is odd. It is not working for me... I mean, this script you have posted worked but not mine on the real page.
@DigitalRobot, well, then maybe you could provide a little more details about your page. Ideally post a sample on jsfiddle illustrating the problem as obviously it is not this function.
@DigitalRobot, in the example you posted you seem to be using jQuery. If this is the case, having a function like the one shown in your question is totally useless. Also your jsfiddle is broken because you are trying to use jQuery but you haven't referenced it so there are plenty of errors.
I am not expert in Javascript, not to mention jQuery. I am not the one who create that page, but I am trying to build a script to interact with that and create some automation. The page was created to accept the creation of new entries on the database. The problem is that you have to do 30 clicks to create one entry and I have 1000 entries to create. At this point, I have 300 entries created and I have to click each one and perform some modifications. So, my idea was to create a CSV file with the modifications and iterate thru the list, clicking each element and doing the modifications.

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.