0

So I have a div class called question_image1 with about four labels inside. My input is wrapped inside of my label <label><input/></label>, so I didn't use the for attribute of a label.

I'm trying to select all labels that weren't the ones that were clicked inside of the class.

$(".question_image1").click(function(event) {
    $('.question_image1 label').not(event.target).style.opacity = "30%";
});

Is there an obvious problem here? One possibility is that there's a level of div between the question_image1 class and the label. Would that be an issue?

1 Answer 1

2

You're trying to use a DOM style property on a jQuery object. Use the jQuery .css() method.

$('.question_image1 label').not(event.target).css("opacity", "30%");

But event.target is will be multiple elements because of event bubbling. So I think you should use

$('.question_image1').not(this).find("label").css("opacity", "30%");

this will be the element that the event was bound to.

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

7 Comments

The code is changing all of the labels, including the one that was the target (confirmed by console.log). Any ideas?
event.target is a DIV, not a label.
console.log prints an img and input element. Either event.target is the label that contains those two, or the propogation makes it so that both elements are clicked, right? How to make it so that only images not clicked will change?
That's event bubbling.
Use $(this) instead of $(event.target). Or maybe $(event.currentTarget).
|

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.