0

So...I am having a bit of trouble trying to create a toggle effect with jQuery that allows for the alternation between two different click events on an li tag.I want the li tag to fade to 0.5 opacity when clicked and when clicked again to revert back to the original state.

Any help would be appreciated and feel free to recreate on jsfiddle :D

The code that I have so far is:

HTML

<div>
    <li><a href="#">Click</a></li>
</div>

jQuery

$(document).ready(function() {
    $('li').toggle();
});

1 Answer 1

2

Check for its current state before proceeding.

Also, <li>'s must have <ul> or <ol> as a parent element.

$('li').on('click', function(e){
  e.preventDefault();
  if($(this).css('opacity') != 1){
    $(this).fadeTo(300,1);
  }else{
    $(this).fadeTo(300,0.5);
  }
});

Demo: http://jsfiddle.net/PZ96m/

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

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.