0

I would like to add a click event to some elements in my HTML code. Here is the code I wrote :

var listsArr = ['list_surgeries', 'list_injuries'];
var divsArr = ['divSurgeries', 'divInjuries']

 for(var i=0; i<listsArr.length; i++){
        $('#'+listsArr[i]).bind("click", {i: i},function() {
            var displayProp = $('#'+divsArr[i]).css("display");
            if(displayProp === "none"){
                $("#"+listsArr[i]).css("list-style-image","url('images/minus.gif')");
            }else if(displayProp === "block"){
                $("#"+listsArr[i]).css("list-style-image","url('images/plus.gif')");
            }
            $('#'+divsArr[i]).slideToggle();
        }); 
    }

I can see the minus and plus gif images added to my ul lists and this shows that the function inside the loop is working however no click event is added.

Is there something wrong with the way I'm adding the event dynamically?

Thanks.

oussama kamal
  • 1,027
  • 2
  • 20
  • 44
  • I recommend to read the jQuery tutorial about event handling: http://learn.jquery.com/events/event-basics/. It explains how to get a reference to the element the event is bound to (via `this`), so you don't even need `i` or the array inside handler. – Felix Kling Oct 09 '14 at 13:32
  • Can you show me a similar question asked please? – oussama kamal Oct 09 '14 at 14:40

0 Answers0