0

I'm having problems with changing the destination folder of my images when clicking a certain button, and changing the src of this image when hovering over another element, whilst retaining the new chosen folder.

The code I have is here, with the jquery excerpt here:

<script type="text/javascript">
$(document).ready(function(){
    imgFldr = 'period-black';

    //click the hardware buttons and change the folder where the images are coming from, but not the image itself (by name)
    $('#standardBlack').click(function(){
        $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardGold').click(function(){
        $("#pic").attr("src",'standard-gold/'+$("#pic").attr("src").split('/')[1]);
    });

    $('#standardChrome').click(function(){
        $("#pic").attr("src",'standard-chrome/'+$("#pic").attr("src").split('/')[1]);
    });

    //on hovering the 21 or 24 colour options, change the colour of the image but not the folder
    $('#black').hover(function(){
        $("#pic").attr("src",imgFldr+"/black.jpg");
    });
    $('#blueGrey').hover(function(){
        $("#pic").attr("src",imgFldr+"/blue-grey.jpg");
    });

    $('#burgundy').hover(function(){
        $("#pic").attr("src",imgFldr+"/burgundy.jpg");
    });

});
</script>

Whats happening is that once you hover over the src change button, the folder goes back to the original variable, but it should keep the chosen folder.

Any ideas on how this could work properly?

tjcss
  • 856
  • 1
  • 12
  • 33

1 Answers1

0

If I understood correctly what the script does:

$('#standardBlack').click(function(){
    $("#pic").attr("src",'standard-black/'+$("#pic").attr("src").split('/')[1]);
    imgFldr = 'standard-black';
});

and so on.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • Thank you Juhana. I have encountered a new problem now. This script worked perfectly locally, but now I'm installing this on the server a few folders deep and I think - in my assumption anyway, that split() is causing a few problems as I've gone a few directories deep. I don't understand what .split('/')[1] does, but Is there a way to only get it to split after the last slash? – tjcss Jul 20 '11 at 14:21
  • `split('/')` creates an array of a string using the / character as a separator. `[1]` selects the second item of that array (as arrays start from 0). See http://stackoverflow.com/questions/3235043/last-element-of-array-in-javascript for some ways to get the last element of the split array. – JJJ Jul 20 '11 at 18:54