variable contains id name, how to select based on variable name

Brad Bansner
Brad Bansner used Ask the Experts™
on
The variable "value" has the name of an ID (build that in line 1). Lines 2 and 3 are attempting to do things to objects with the ID of what the variable contains. But I get an error:

$('#'+value).attr('id').addClass is not a function
var value=$(this).attr('id').replace('dir_', 'day_');
$('#'+value).attr('id').addClass('dayblockhl');
$('#'+value).attr('id').children('.gr_mwc_monthblockarrow').addClass('gr_mwc_monthblockarrowhl');

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

it looks like you have a couple things going on here.

1. you are changing the id of the item you are trying to work with, so if it was id of "testdir_1" you are retrieving testday_1, are you sure an element with the changed id exists?

2. you are trying to add a class to an attribute. Attributes do not have an addClass function, try:


assuming that line in your code above is valid
var value=$(this).attr('id').replace('dir_', 'day_');
$('#'+value).addClass('dayblockhl');
$('#'+value).children('.gr_mwc_monthblockarrow').addClass('gr_mwc_monthblockarrowhl');

Open in new window

why are you putting in  the .attr('id')? it  looks like you are trying to do everything to the id attribute not the actual element.
leakim971Multitechnician
Top Expert 2014

Commented:
@haloexpertsexchange's right (no point please) :
var value=$(this).attr('id').replace('dir_', 'day_');
$('#'+value).addClass('dayblockhl');
$('#'+value).children('.gr_mwc_monthblockarrow').addClass('gr_mwc_monthblockarrowhl');
//$('#'+value).find('.gr_mwc_monthblockarrow').addClass('gr_mwc_monthblockarrowhl');

Open in new window

Brad BansnerWeb Developer

Author

Commented:
Yes, you were both right... two different ways of saying the same thing. I left that attr('id') in by accident. I am slowly learning how to do this stuff on my own. Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial