jQuery changing the source folder of multiple images
-
I'm trying to have my images source attributes to change when a certain button is clicked. I got this code as of the moment. $('.palettemain #brown').click(function () { var imageName = $('.panestop span > img').attr('alt'); var imageFolder = 'images/sitebuilder/logo/brown/'; $('.panestop span > img').each(function () { $(this).attr('src', imageFolder + imageName); }); }); As you can see I have declared variable names for the image name and the image folder. when I click .palettemain #brown (the button) it loads the image, also fetching the correct folder name and image file name. The thing here is, only the first instance image file name is fetched. Below is the HTML code before the click happens. <span> <input type="radio" name="logo" checked="checked" /> <img src="images/sitebuilder/logo/default/logo01.png" alt="logo01.png" /> </span> <span> <input type="radio" name="logo" checked="checked" /> <img src="images/sitebuilder/logo/default/logo02.png" alt="logo02.png" /> </span> <span> <input type="radio" name="logo" checked="checked" /> <img src="images/sitebuilder/logo/default/logo03.png" alt="logo03.png" /> </span> Below is the output when clicked: <span> <input type="radio" checked="checked" name="logo"> <img alt="logo01.png" src="images/sitebuilder/logo/brown/logo01.png"> </span> <span> <input type="radio" checked="checked" name="logo"> <img alt="logo02.png" src="images/sitebuilder/logo/brown/logo01.png"> </span> <span> <input type="radio" checked="checked" name="logo"> <img alt="logo03.png" src="images/sitebuilder/logo/brown/logo01.png"> </span> The output, as you can see, is duplicating the first image alt attribute. How do I fetch the alt attribute for each instance of the image?
-
Answer:
You only read the image name once from the alt attribute. Try: $('.palettemain #brown').click(function () { var imageFolder = 'images/sitebuilder/logo/brown/'; $('.panestop span > img').each(function () { imageName = $(this).attr('alt'); $(this).attr('src', imageFolder + imageName); }); });
shingou at Stack Overflow Visit the source
Other answers
will using alt in this fashion cause problems? I have a similar system using data-name="image.jpg" that still leaves alt available for what it is meant to be used for, an text description to visual image.
nosarious
Related Q & A:
- How are multiple images stitched?Best solution by stackoverflow.com
- How to drag multiple images in Android?Best solution by Stack Overflow
- How to store multiple Images in database?Best solution by Stack Overflow
- how to embed multiple images into HTML email?Best solution by Stack Overflow
- How do i get multiple images?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.