tags:

views:

71

answers:

3

I would prefer to use .slideToggle and 3 lines of code but not sure how to get the script to 'refresh' itself after it's slid open and return to it's orginal state when it's slid back up (it doesn't slide back up when I use 3 lines and a .slideToggle). Sorry for the bad technical explanation.

$('#showHide').hide();

$('a#slickShow').click(function() {
    $('#showHide').show(400);
    $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View less Blog Entries<span class="archiveLink2">{</span></a>');

$('a#slickShow').click(function() {
    $('#showHide').hide(400);
    $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View more Blog Entries<span class="archiveLink2">}</span></a>'); 

This is the code that eventually got it working

$('#showHide').hide();

$('#slickShow').click(function(){

    $('#showHide').slideToggle(400, function() {
        if ($('#showHide').is(':visible')){
          $('.archiveText2 a').html('View less Blog Entries<span class="archiveLink2">{</span>');

        }
        else{
          $('.archiveText2 a').html('View more Blog Entries<span class="archiveLink2">}</span>'); 
        }

    }); 

    return false; 
});
+1  A: 

As far as I could understand it, try this:

$('a#slickShow').click(function(){
  $('#showHide').slideToggle(400);

   if ($('#showHide').is(':visible')){
     $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View less Blog Entries<span class="archiveLink2">{</span></a>');
   }
   else{
     $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View more Blog Entries<span class="archiveLink2">}</span></a>'); 
   }

   return false;
});
Sarfraz
That doesn't slide back up again though, that's the trouble I had when I tried an if/else statement, it slides down and replaces the text perfectly but then the link won't slide it back up, it just dies
Dan
@Dan: See my updated answer with added `return false` line.
Sarfraz
Thanks for the help, still doesn't slide up though?
Dan
I got it working by putting the IF statement in a callback function. It wasn't working because animation effects have a time delay so the IF statement can't be tested before the end of the delay. I've amended my code to show the working script
Dan
A: 
$('#showHide').hide();


$('a#slickShow').toggle(
      function(){
          $('#showHide').show(400);
          $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View less Blog Entries<span class="archiveLink2">{</span></a>');
      },
      function(){
          $('#showHide').hide(400);
          $('.archiveText2 a').replaceWith('<a id="slickShow" href="#">View more Blog Entries<span class="archiveLink2">}</span></a>'); 
      }
);
From.ME.to.YOU
A: 

Hi Dan,

Looks like you are replacing the whole anchor when all you need to do is replace the html inside the anchor. .toggle() can be used to set up functions that work on alternate clicks. Also you can drop the 'a' from the 'a#slickShow' selector, as the ID lookup alone will be faster.

$( "#slickshow" ).toggle(
  function() {
    $(this).show(400).html( "View less Blog Entries<span class="archiveLink2">{</span>" );
  },
  function() {
    $(this).hide(400).html( "View more Blog Entries<span class="archiveLink2">}</span>" );
  });

Cheers, awirick

Andrew Wirick
Thanks for the help, I dropped the 'a'. I can now see I don't need to replace the whole anchor, but couldn't get the '.html' to work?
Dan
Got it working now so thanks for the advice
Dan