I’m doing an hobby HTML project with lots of PHP, Jquery into it.
I finally got my sorting working:
http://www.spisbilligt.nu/.
Now i need the picture to change when I have sorted the list and click one of the remaining buttons.
I got my code here:
http://jsfiddle.net/6qNAt/1/
var itemInfo = { // Initialise the item information
item1: ['item1.jpg', 'Description of item 1...'],
item2: ['item2.jpg', 'Description of item 2...'],
...
};
(function() {
('#items a').click(function() { // When an item is selected
var id = $(this).attr('href').replace(/#/, ''); // Retrieve its ID
('#info img').attr('src', itemInfo[id][0]); // Set the image
('#info p').text(itemInfo[id][1]); // And text
return false; // Prevent default behaviour
});
});
To see what i am meaning and get hungry look here and see the cupcakes:
http://www.georgetowncupcake.com/menu.html
I would encourage you to move your data to
data-*attributes on the list-items themselves:Then use event-delegation to listen for any click:
And lastly, set the data attribute values to the image and text within the info area:
Fiddle: http://jsfiddle.net/6qNAt/3/