It's sort of clear what you're trying to do, but it's not at all clear why you're trying to do it. Variables should, well, vary... but their names should not. (This comes up a surprising number of times, actually, so I really wonder where the impetus to do this comes from.)
This is not a variable name:
$(this).attr(id)
It's a call to the $
function, which returns a jQuery object, followed by a call to the attr
function on that object, which returns a value. (Or should, depending on what id
is in this context. If you meant 'id'
instead then it should return the string value of the id
attribute on the matched element.)
This should work, in that it should alert the string value of the id
attribute:
alert($(this).attr(id));
But the question is, what are you trying to do by using a function call's return value as a variable name? I know JavaScript isn't a static language, but it's not that dynamic.
The variable name has to be static, otherwise how would any other code know how to use it? There is undoubtedly a better structure to achieve the functionality you're looking to achieve.