javascript - 'undefined' function when triggering a click of a link with jQuery - Stack Overflow

admin2025-04-26  22

For some reason this works in FF 11, Chrome, and even in IE but not in FF 3.6 or Safari 5.

Basically I'm triggering the click of a link with:

var $currentItem = $('#' + currentId);
var modalSeriesList = $('.js-modal-series');
var index = modalSeriesList.index($currentItem);
var $nextItem = modalSeriesList[index + 1];

var nextClick = function() {
    $(document).off('keydown.general');
    $nextItem.click();
}

$(document).off('keydown.general');

if ($nextItem) {
    $nextButton.click(nextClick);
}​

But when I click on the link in FF 3.6 or Safari 5 I get the following error:

TypeError: 'undefined' is not a function (evaluating '$nextItem.click()')

Is there any particular "gotcha" that I don't know about here using the following method?

For some reason this works in FF 11, Chrome, and even in IE but not in FF 3.6 or Safari 5.

Basically I'm triggering the click of a link with:

var $currentItem = $('#' + currentId);
var modalSeriesList = $('.js-modal-series');
var index = modalSeriesList.index($currentItem);
var $nextItem = modalSeriesList[index + 1];

var nextClick = function() {
    $(document).off('keydown.general');
    $nextItem.click();
}

$(document).off('keydown.general');

if ($nextItem) {
    $nextButton.click(nextClick);
}​

But when I click on the link in FF 3.6 or Safari 5 I get the following error:

TypeError: 'undefined' is not a function (evaluating '$nextItem.click()')

Is there any particular "gotcha" that I don't know about here using the following method?

Share Improve this question asked Jun 6, 2012 at 23:28 bob_cobbbob_cobb 2,26911 gold badges52 silver badges115 bronze badges 2
  • It's just looks like you got outside of the array index. – gdoron Commented Jun 6, 2012 at 23:31
  • should nextClick be nextClick()? – wirey00 Commented Jun 6, 2012 at 23:37
Add a ment  | 

3 Answers 3

Reset to default 4

Try .eq() so $nextItem is a jQuery collection:

var $nextItem = modalSeriesList.eq(index + 1);

// ...

$nextItem.click()

Using [...] retrieves the DOM Object, which doesn't have a .click() method.

You could try

if( typeof $nextItem == 'undefined' ){
    $nextButton.trigger('click');
}

instead of

if ($nextItem) {
    $nextButton.click(nextClick);
}​

if you turn condition of if statement into

if(typeof $nextItem == 'undefined' )

it works. As Porco said probably $nextItem does not exist....

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745648716a312034.html

最新回复(0)