2

Based on this I am trying to add the click event handler to an array of objects by writing this:

function addEventHandler(array, type, func) {
      var len = array.length;
      for (var i = 0; i < len; i++) {
         array[i].bind(type, func);
      }
   }

sections = $('#sponsorship > div .section')

addEventHandler(sections, 'click', function() {
      console.log(this);
});

However, I am getting the error message:

array[i].bind is not a function

Can I only use actual selectors on a bind method? Any suggestions?

1
  • 1
    have a look at each() - instead of your loop Commented Nov 25, 2011 at 18:59

6 Answers 6

5

You may need to convert your element into a jQuery object.

$(array[i]).bind
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is a bit strange as it takes a jQuery object, turns it into a regular JS object, and then converts it back to a jQuery object. Check-out Royi Namir's solution.
3

try this

function addEventHandler(array, type, func) {
      var len = array.length;
      for (var i = 0; i < len; i++) {
         array.eq(i).bind(type, func);
      }
   }

3 Comments

I'm curious, what is this supposed to fix?
@jasper array is array of jquery elements. when he to array[0] he get an item which is NOt jquery but pure JAVASCRIPT object which he CANT use the BIND method of jQUERY. but if he use array.eq(i) - he can treat the item as jquery item and BIND him method.
I see, this is a good call, rather than turning a jQuery object into a regular JS object and then back into a jQuery object. +1.
1

The error message you are getting is exactly what you'd get by trying to run jQuery functions on non-jQuery objects. You can easily fix this by using the index of your for loop to access the jQuery objects in the array variable:

function addEventHandler(array, type, func) {
      var len = array.length;
      for (var i = 0; i < len; i++) {
         array.eq(i).bind(type, func);
      }
   }

sections = $('#sponsorship > div .section')

addEventHandler(sections, 'click', function() {
      console.log(this);
});

Changing array[i].bind(type, func); to array.eq(i).bind(type, func); accesses the jQuery object rather than a regular JS object which will remove the error you're getting.

Here is a jsfiddle of the above change to your code: http://jsfiddle.net/jfUpB/1/

Comments

0

I'd replace the for(int...) by jQuery's $.each function as you'd be dealing with the item of the array and not the trying to retrieve the item by index. Also, to use any jQuery function, your object inside the array should be a jQuery object, so I'd do this:

function addEventHandler(array, type, func) {
    var len = array.length;
    $(array).each(function(index, item) {
        $(item).bind(type, func);
    });
}

Comments

0
$('#sponsorship > div .section').click(function (event) {
   console.log(event.target);
});

or

$('#sponsorship > div .section').each(function(index,item) {
    $(item).click(function (event) {
       console.log(event.target);
    });
});

Whats wrong with this ?

Comments

0

.each() is performance heavy, and considered 'lazy' even by jQuery's standards.

Comments

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.