0

I'm trying to make my link toggle from left to right. I'm using the code below;

HTML:

$forum_threads = '
    <div id="latest_threads_link">
        <a href="javascript:void(0)" id="latest_threads_click"><img src="images/kalachi/latest_threads.png" alt="Latest Threads" title="Click Here to see latest threads of this section."></a>
        <table border="0" cellspacing="'.$theme['borderwidth'].'" cellpadding="'.$theme['tablespace'].'" class="tborder" style="display: none;" id="latest_threads_show">
            '.$forum_threads_bit.'
        </table>
    </div>
';

jQuery:

jQuery(document).ready(function($){
  $('a[id^="latest_threads_click"]').on('click', function (e){
        e.preventDefault();
        $('#latest_threads_show').slideToggle();
        $('#latest_threads_click').addClass('latest_threads_link_active');
    });
});

CSS:

#latest_threads_link{
    left:0;
    position:fixed;
    bottom:150px;
}

.latest_threads_link_active{
    left:200px;
    position:fixed;
    bottom:150px;
}

I want to make it so when #latest_threads_link is clicked then #latest_threads_show toggleout from left to right and on second click on #latest_threads_link then the table #latest_threads_show hides.

It actually does with my code but the issue is the #latest_threads_link remains on 200px away from left even on second click. I want #latest_threads_link to go left: 0 on second click.

Please help!!

3
  • can you add your generated html ?then we can test it locally Commented Jan 10, 2014 at 14:08
  • .toggleClass('classname') is the solution. Commented Jan 10, 2014 at 14:10
  • Sad to see none of the answers pointing out $('#latest_threads_click').slideToggle() is just $(this).slideToggle() and $('a[id^="latest_threads_click"]') is faster as just $('#latest_threads_click') (assuming there aren't other id's starting with latest_threads_click) Commented Jan 10, 2014 at 15:12

3 Answers 3

2

Your problem is that you're adding the class on each click. Instead, change this line:

$('#latest_threads_click').addClass('latest_threads_link_active');

to this:

$('#latest_threads_click').toggleClass('latest_threads_link_active');
Sign up to request clarification or add additional context in comments.

Comments

1

use toggleClass :

Demo : http://jsfiddle.net/5Y9mG/6/

jQuery(document).ready(function($){
  $('a[id^="latest_threads_click"]').on('click', function (e){
        e.preventDefault();
        $('#latest_threads_show').slideToggle();
        $('#latest_threads_click').toggleClass('latest_threads_link_active');
    });
});

Comments

0

Replace

$('#latest_threads_show').slideToggle();

with

$('#latest_threads_show').slideToggle(400, function()
{
    if($(this).is(":hidden"))
    {
        $(this).css({'left':'0px;'});
    }else
    {
        $(this).css({'left':'200px;'});
    }
});

It changes the left from 0px to 200px and repeat, etc (toggling).

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.