Peeyush
Peeyush

Reputation: 4828

Why this jquery selector gives me error? uncaught exception: Syntax error, unrecognized expression: ''

I am using this code ti dynamically select attributes of a element but it gives me an error in firebug

Error:

uncaught exception: Syntax error, unrecognized expression: ''

Here is my code:

jQuery('.mydata').click(function(){

    var current_id=jQuery(this).attr('id');
    var current_datatype=jQuery(this).attr('datatype');

    var next_id=parseInt(current_id);

    next_id=next_id+1;

    next_id="'#"+next_id+"'";

    var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error

});

Upvotes: 0

Views: 96

Answers (5)

Svante Svenson
Svante Svenson

Reputation: 12478

Your selector becomes something like '#2' instead of #2. You need to remove the extra '. Also you should never use parseInt without passing in a radix, like parseInt(currentId, 10).

Upvotes: 0

Antony Scott
Antony Scott

Reputation: 21996

change this ...

next_id="'#"+next_id+"'";

to this ...

next_id="#"+next_id;

Upvotes: 1

PeterMmm
PeterMmm

Reputation: 24630

I think you don't need the extra quotes here

next_id="'#"+next_id+"'";

should be read

next_id="#"+next_id;

Upvotes: 1

Tim
Tim

Reputation: 9489

when selecting an id you don't need the quotes if you assign it to a variable

change:

next_id="'#"+next_id+"'";
var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error

into:

next_id="#"+next_id;
var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error

Upvotes: 4

janhartmann
janhartmann

Reputation: 15003

What if you do

next_id = "#" + next_id; instead of next_id="'#"+next_id+"'";

Upvotes: 4

Related Questions