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

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

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....

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648715a4638127.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信