2

I am trying to use jQuery's addClass() to add the class "picbox_selected" to the items in the array features, which have id numbers corresponding to the index of items in the array config. I tried the following code but it is not working.

var leftHandShape = features['Left Hand Shape']
for (i = 0; i < config.HandShapes.length; i++) {
    var handName = config.HandShapes[i].Name;
    if (leftHandShape == handName) {
        var idNo = '.picbox id' + i
        $(idNo).addClass('picbox_selected')
    }
}

I tried this as well but it's not working either. Please help!

var leftHandShape = features['Left Hand Shape']
for (i = 0; i < config.HandShapes.length; i++) {
    var handName = config.HandShapes[i].Name;
    if (leftHandShape == handName) {
        $(document).ready(function() {
            $('.picbox id' + i).addClass('picbox_selected')
        })
    }
}
5
  • 3
    "It doesn't work" is not a problem statement. State the behavior you are actually getting. Commented Aug 6, 2015 at 17:46
  • the class is not getting added Commented Aug 6, 2015 at 17:50
  • Pickbox id does not work. You cannot have space in class and ids Commented Aug 6, 2015 at 17:51
  • @Jkike: Suggest something that does work. Commented Aug 6, 2015 at 17:52
  • Ok... first suggestion: $('.pickbox id') will search for an element with class pickbox ... what is id then? Remove the whitespace from classname first. Commented Aug 6, 2015 at 17:55

2 Answers 2

1

Currently, you have this:

$('.picbox id' + i).addClass('picbox_selected')

Lets take a look at the selector '.picbox id':

  1. Find all elements with class picbox
  2. Find all <id> elements.

Woah! Wait a second! You REALLY want that? Or you meant '.picbox #id'?


Look at '.picbox #id' we have:

  1. Find all elements with class picbox
  2. Find all elements with id="id".

Is this what you were trying to do?

Sign up to request clarification or add additional context in comments.

2 Comments

I have class names called id0, id1, id2, etc. But thanks for the suggestion!
@sleepycow Then instead of #id, use .id which should do it
0

Are you trying to do it for all of them?

You could use a simple javascript for loop

for (i = 0; i < features.count; i++){
  var item = features[i];
  item.className = "picbox_selected";
};

1 Comment

I want to add the class for all items in the array feature, but not the array config. I might try the className approach though!

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.