0

i am using the following javascript function

window.onload = ajaxreq;
function ajaxreq()
    {

        test = document.getElementsByClassName("test_button")

         for (var i =0;i<test.length;i++)
     {
         test1 = test[i];
         if(test[i].onclick == true)
         {
            alert("i")
             }

     }

    }

but it alerts nothing Please tell me how to get the value of i for the element clicked

  • 1
    this is not going to work. try adding an eventlistener for the click event. – Sushil Jul 24 '15 at 17:43
  • how to do that please tell – Kushagra Singh Rajput Jul 24 '15 at 17:45
  • it should be `test[i].addEventListener('click', yourFunction);` try reading up on it. http://www.w3schools.com/jsref/met_element_addeventlistener.asp – Sushil Jul 24 '15 at 17:49
  • @Sushil First, addEventListener is not supported by IE8 (still industry standard). Second don't post references to w3schools, they are usually not received well by the community because often they will contain out-of-date or plain incorrect information. Instead reference a w3c specification document, the w3c wiki, or a reputable source like MDN. –  Jul 24 '15 at 18:05
  • [`for(...){(function(i){test[i].onclick=function(){alert(i);}})(i);}`](http://jsfiddle.net/1L5Lqboy/) –  Jul 24 '15 at 18:19
  • @TinyGiant a lot of things are not supported by IE 8 that doesn't mean u stop using it. as far as the w3schools link is concerned, that was the first link on google search. i myself don't refer it. that is why i told him to try reading up on it. – Sushil Jul 24 '15 at 18:34

0 Answers0