0

I am using .map to get an array of element IDs (this is named 'ids') that have a 'default-highlight' class. After removing that class on mouseenter, I want to return that class to those specific id's (basically, leave it how I found it).

Two things are causing me trouble right now:

  1. When I dynamically add data-ids to the td elements and then use those data-ids to create the array of 'ids' my mouseenter stops adding the 'HIGHLIGHT' class (NO idea why this is happening)
  2. On mouseleave I can't loop through the 'ids' and return the 'default-highlight' class to the elements they originally were on

I figure I should be using something like this, but it obviously isn't working:

$.each(ids, function() {
   $(this).addClass('default-highlight');
});

I have tried a number of things, but keep coming up short. I am attaching a link to a codepen.io where I use data-ids that are being dynamically added to the table (this one the mouseenter doesn't work) and a codepen one where I am using regular IDs for the default highlight and everything appears to work like it is supposed to be (It isn't, since I want to be using the dynamically generated data-ids and then the subsequently produced array to reapply those classes).

Both of these codepens have a gif at top showing how the interaction should work.

If anything is unclear, please let me know. Thanks for reading!

2
  • Uncaught TypeError: ids.get is not a function. Replace $('#output').text(ids.get().join(', ')); with $('#output').text(ids.join(', ')); codepen Commented Sep 21, 2015 at 18:15
  • Thanks @ᴀʀᴛᴜʀғɪʟɪᴘɪᴀᴋ for pointing that out - fixed it Commented Sep 21, 2015 at 18:18

3 Answers 3

4

You need to add # before id selector

$.each(ids, function() {
   $('#'+this).addClass('default-highlight');
});

or you can use common selector by the help of map() and join()

$(ids.map(function(i, v) {
  return '#' + v;
}).join()).addClass('default-highlight');

or you can add # when getting the id's and then you just need to join them

var ids = $('.default-highlight').map(function(i) {
  return '#'+$(this).data('id');
}).get();
...
...
...
$(ids.join()).addClass('default-highlight');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, giving this a shot
@NealJones : glad to help
1

It seems like storing the IDs and using those is overkill when you can store a reference to the jQuery element directly:

$highlightCells = $('.default-highlight').removeClass('default-highlight')

And later give the class back:

$highlightCells.addClass('default-highlight')

Here's a codepen fork: http://codepen.io/anon/pen/ZbOvZR?editors=101

Comments

0

Use this way:

$.each(ids, function() {
   $("#" + this).addClass('default-highlight');
});

3 Comments

this in string array isn't what you want it to be
This will be the same as mapping all the array elements with the # and adding the class right?
not sure why...but $.each doesn't assign the array element to this when it is a string, only object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.