1

Does anyone know why this jquery might not work?

Ultimately I will replace the "1" index with a variable but I can't even get this working at the moment.

$('.myClicker').click(function() {

    $("#selectBoxContainer img").css({"background-color":"#FFF"});
    $("#selectBoxContainer img:eg(1)").css({"background-color":"#000"});

});
0

3 Answers 3

2

You should use eq(), not eg(). Also, I usually use another css() syntax, not sure if your will work. Try this:

$("#selectBoxContainer img:eq(1)").css('background-color', '#000');

1 will be the second element, 0 is the first.

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

1 Comment

.css() syntax seems to be fine.
2

You need the eq selector, not eg

Comments

2

All the previous answers regarding the misnamed eq are correct. As some additional info for you, if you are really using a variable for the index number you might find it easier using the eq method instead of string concatenating the selector.

This is always easier IMO:

var index = 1;
$("#selectBoxContainer img").eq(index).css({"background-color":"#000"});

Than this:

var index = 1;
$("#selectBoxContainer img:eq(" + index + ")").css({"background-color":"#000"});

Comments

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.