Dan Rubio Dan Rubio - 1 year ago 58
CSS Question

Why am I unable to find an html element through jQuery/JS string interoplation?

I have a series of div blocks each with a unique numerical id.

For example:
enter image description here

As part of a project I'm working on, I want to select the 29th element after the element I choose. So if I chose the first div with id of

then I want to be able to choose div with id
. Here is how I am attempting to do this with the first div:

let int = parseInt($('.blockattribute').first().attr('id')) + 29;
$('#' + toString(int);

This gives me the following error:

Uncaught Error: Syntax error, unrecognized expression: #[object Undefined]

This should work though. By executing
, I'm able to select the div I want. What could be the problem?

Answer Source

You're using Object.prototype.toString which would return [object Undefined] when you do


you wanted to use Number.prototype.toString() instead, and you'd do that like so

let int = parseInt($('.blockattribute').first().attr('id')) + 29;
$('#' + int.toString());

but you really don't need it, as concatenating a string and a number returns a string anyway

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download