Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find & replace jquery

Tags:

jquery

I have this code to give me a rollover on submit buttons, and I'm trying to make it more generic:

$('.rollover').hover(
            function(){ // Change the input image's source when we "roll on"
                srcPath = $(this).attr("src");
                            srcPathOver = ???????
                /*need to manipulate srcPath to change from
                img/content/go-button.gif
                into 
                img/content/go-button-over.gif
                */
                $(this).attr({ src : srcPathOver});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : srcPath});
            }
     );

Two things really,

I want to learn how manipulate the srcPath variable to append the text '-over' onto the gif filename, to give a new image for the rollover. Can anyone suggest a way to do this?

Also, can someone tell me if this code could be refined at all? I'm a bit new to jQuery and wondered if the syntax could be improved upon.

Many thanks.

like image 415
Chris J Allen Avatar asked Oct 24 '08 15:10

Chris J Allen


1 Answers

$('.rollover').hover(
            function(){ // Change the input image's source when we "roll on"
                var t = $(this);
                t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1-over.$2"));
            },
            function(){ 
                var t= $(this);
                t.attr('src',t.attr('src').replace('-over',''));
            }
     );
like image 163
jcampbell1 Avatar answered Nov 15 '22 11:11

jcampbell1