Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - unrecognized expression: :nth-child

Tags:

jquery

I am trying to select :nth-child using jquery, but it show syntax error.

Error: Syntax error, unrecognized expression: :nth-child

Here is my code

var i;
jQuery('#' + id + ' .tab-pane').each(function (id, t) {
    var n = jQuery(this).attr('id', 'pane-' + t);
    var p_id = n.attr('id');
    jQuery('#' + id + ' .nav-tabs li:nth-child(' + i + ') a').attr('href', p_id);
    i++;
});

please check my code what is missing here

like image 686
user007 Avatar asked Jul 25 '13 15:07

user007


1 Answers

On the first iteration, there is no value for i. So the query looks like this:

jQuery('#' + id + ' .nav-tabs li:nth-child(undefined) a').attr('href', p_id);

On following iterations, it will be undefined++, which is NaN, which still won't work.

This obviously won't work. The solution is to set i to 1 (or to whatever value is necesary) on the first loop:

var i = 1;
like image 194
lonesomeday Avatar answered Sep 21 '22 04:09

lonesomeday