ab11 ab11 - 6 months ago 9
Javascript Question

jQuery: how to retrieve element by concatenated ID?

I am dynamically creating and generating their id's to be something like

hidden1234
.

In the below Javascript code,
var hid
is retrieved properly by
document.getElementById
. But the jQuery line below has a syntax error. How can I retrieve this element by Id using jQuery?

var hid = document.getElementById('hidden'+id);
var hidden = $("#'hidden'+id");

Answer

It's just a simple typo:

var hidden = $('#hidden'+id);

You want to select an element with the id 'hidden' + id. The according CSS / jQuery selector therefore looks like '#hidden' + id.