0

Is there any way to combine all of this to reduce the amount of javascript?

    $(document).ready(function() {

    $(".individualImagebox img").bind("click", function()
    {
        var src = $(this).attr("src");

        if (src.search(/Red\.jpg$/) >= 0) return;

        // Removes the red overlay from the images folder
        $('.individualImagebox img').attr( "src", function () {  
            var thisSRC = $(this).attr( "src");
            return thisSRC.replace(/Red\.jpg$/, ".jpg");
        });

        // Adds the red overlay from the images folder
        $(this).attr( "src", src.replace(/\.jpg$/, "Red.jpg")  );
    });

});

function ShowHide(index) {

    var itemSelector = ".name:eq(" + index + ")";

    $(".name .bio").fadeOut();
    $(".name").not(itemSelector).fadeOut();

    $(itemSelector).animate({"height": "show"}, { duration: 500 });
    $(itemSelector + " .bio").animate({"height": "show"}, { duration: 500 });

}
1
  • Is ".jpg" an image? If not, why not just remove the src attribute? Commented Mar 29, 2011 at 15:12

2 Answers 2

1
$(function() { // $(function(){}) is a shortcut of $(document).ready(function(){})

    var $activeImg; // Maintain a reference to the last activated img

    $(".individualImagebox img").click(function(){
        if (!!$activeImg) {
            $activeImg.attr("src", function(i, src){
                return src.replace(/(.+)Red\.jpg$/, "$1.jpg");
            });
        }
        $activeImg = $(this).attr("src", function(i, src){ // replace attribute and updates active img reference
            return src.replace(/(.+)\.jpg$/, "$1Red.jpg");
        });
    });
});

I don’t know exactly what you are trying to do but if possible, you should toggling a class instead of modifying the src attribute.

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

1 Comment

There are two files and one needs "red" at the end of them. For instance chuck.jpg and chuckRed.jpg
1

Combining methods won't save you space. Take a look at

http://developer.yahoo.com/yui/compressor/

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.