0

I have many divs with class "mypoll". Here is my js code:

var mypoll=[];

$(document).ready(function (){      
    mypoll=document.getElementsByClassName("mypoll");
});

for(var t=0; t<mypoll.length; t++) {
    mypoll[t].getElementsByClassName("deletePoll")[0].onclick=(function() {
        var currentI = t;
        return function() { 
            deletedMyPoll(currentI);
        };
    })();
}
function deletedMyPoll(i){
    var a=document.getElementsByClassName("mypoll")[0].innerHTML;
    alert(a);
}

The error I get is

cannot set property onclick of undefined. 

I have the exact same piece of code on my project (it is big and complicated and I can't post it all here) but it doesn't work.

It works on the jsfiddle though.

Is there any problem with the global variables in javascript? Can I use them that way?

jsfiddle.net/Vwtmu

Thank you

4
  • 2
    let's start with properly indenting Commented Feb 8, 2013 at 6:42
  • 1
    he meant next time you should improve code formatting before posting the question ;) Why don't you share the fiddle with us? Commented Feb 8, 2013 at 6:51
  • right! jsfiddle.net/Vwtmu Commented Feb 8, 2013 at 6:55
  • 2
    Your fiddle does not work because you did not let it use jQuery. Take a look at the console and you will see Uncaught ReferenceError: $ is not defined Commented Feb 8, 2013 at 7:01

2 Answers 2

1

If you're going to use jQuery, you might as well use it:

$(".mypoll").on('click', '.deletePoll', function () {

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

2 Comments

ok but it's just that i don't know at all jquery and i ll mess up the whole project.
can i use what you wrote and pass a parameter in the onclick function?
0

The for loop runs before the dom is ready.

$(document).ready(function (){      
    mypoll=document.getElementsByClassName("mypoll");

    for(var t=0; t<mypoll.length; t++) {
        mypoll[t].getElementsByClassName("deletePoll")[0].onclick=(function() {
            var currentI = t;
            return function() { 
                deletedMyPoll(currentI);
            };
        })();
    }

});

4 Comments

Please read what the error message is. It has nothing to do with whether mypoll is set or not. If that was the case, the for loop wouldn't iterate, because mypoll.length would be 0. The error message is "cannot set property onclick of undefined". The only place onclick is used is on mypoll[t].getElementsByClassName("deletePoll")[0], which means THAT is undefined
i put some of the loops in $(document).ready(function (){ and it worked! Do I have to do it for every loop? I am saying that because all the loops are already in a big $(document).ready(function (){
actually it does have to do with you poll, document.getElementsBy* will not work if the actual elements is not on the dom yet. have you tried using my exact code?
I used almost the same as yours. I ll try again and come back to you. SO , we are sure that js reaches the dom before it is created? Even if i have the whole code (not only the loop itself) wrapped with $(document).ready( ??? thank you

Your Answer

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