1

In this script, when the "Show" link is clicked, and the preview image is shown, the link text should change to "Hide". It only works after the 3rd click.

<span id='previewLinks'><br/><a id='showPreview' title='".get_option('my_theme')."'>Show</a>

$('#showPreview').click
(
    function(e) 
    {
    var myImage = $('#my_theme :selected').val();
    $('#largePreview').slideToggle(0, function() {
        $('#showPreview').text($(this).is(':visible') ? 'Hide' : 'Show');
    });
    $('#largePreview').attr('src', '<?php echo get_bloginfo('template_directory') ?>/styles/'+myImage+'/screenshot-help.jpg');
    }
);
2
  • Is #largePreview initially shown or hidden? Commented Jan 19, 2012 at 19:11
  • Any reason you are using slideToggle with a duration of zero instead of hide()? Commented Jan 19, 2012 at 19:24

3 Answers 3

2

you can try this:

$('#showPreview').click(
    function(e) {
    var myImage = $('#my_theme :selected').val();
    $('#largePreview').slideToggle(0, function() {
        $('#showPreview').text($(this).is(':visible') ? 'Hide' : 'Show');
    });
    $('#largePreview').attr('src', '<?php echo get_bloginfo('template_directory') ?>/styles/'+myImage+'/screenshot-help.jpg');
    }).click();
Sign up to request clarification or add additional context in comments.

1 Comment

@Scott B here i add an initial click()
1

The reason it doesn't work initially is because #largePreview must have a display state opposite from what you are assuming. This change may fix it. Note the !

$('#showPreview').text(!$(this).is(':visible') ? 'Hide' : 'Show');

Or you may need to change the css for #largePreview.

2 Comments

That's kind of anti-intuitive isn't it? IF hidden THEN output "hide", IF visible Then output "show".
@Jasper - yeah, I answered a question like this a few days ago and the user actually wanted it that way. Confused me, but I guess some people view it that way.
0

just figure out what you did wrong:

See example here.
1. Use

$(document).ready(function(){
});

2. <a href="javascript:;"> TEXT </a> will give you a right anchor
Hope it helps! Thanks to Jasper Too.

1 Comment

$(this).filter(":visible") always returns true so the text is always set to "Hide". Using .is() instead of .filter() will return true/false: $(this).is(":visible"). You can also check the length property of the jQuery object after filtering down to only visible elements: $(this).filter(":visible").length > 0. Here is an updated version of your jsfiddle: jsfiddle.net/Dgc3t/5

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.