1

I want to be able to click on any div from an array and have the box change it's opacity. I have a jsfiddle to explain better what I mean. I have the code working with plain javascript but I don't know how to do the same with jquery. Thanks for your help!

$('.mind:eq(i)').click(function () {
    $('.mind:eq(i)').css('opacity', '1');
});

//Plain javascript
var box = document.getElementsByClassName('box');
function change(i2) {
    box[i2].onclick = function () {
        box[i2].style.opacity = "1";
    };
}
for (i = 0; i < box.length; i++) {
    change(i);
}
2
  • Do you want to get rid of the for loop or just have a general function that changes the opacity on click? Commented Jun 5, 2015 at 15:14
  • Just a general function that changes the opacity on click. That's the only way I knew how by using plain javascript. Commented Jun 5, 2015 at 15:17

1 Answer 1

4

Use this for current selected element

$('.box').click(function () {
    $(this).css('opacity', '1');
});

Updated fiddle

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

1 Comment

Wow. I had no idea it would be that simple. Thanks!

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.