Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using jquery replace the last 2 letters before .jpg in a link path of a anchor tag

Posted on 2014-09-15
11
Medium Priority
?
239 Views
Last Modified: 2014-10-03
Hi Everyone,

I am currently display product additional image like below. You'll notice that the anchor tag href is basically the same name except the last 2 letters before the .jpg is changed to lg or Lg or LG from sm or Sm or SM

I can't hardcode them as they are dynamically called from the db for the image tag, I am trying to populate the anchor href using these values but need to change those 2 letters.

Any advice or example is appreciated


<div class="product-images">
<ul id="thumbnails1" class="thumbnails">
<li><a href="graphics/00000001/TKEco-lg.jpg"><img src="graphics/00000001/TKEco-sm.jpg"  /></a></li>
<li><a href="graphics/00000001/ToolApronLg.jpg"><img src="graphics/00000001/ToolApronSm.jpg" /></a></li>
</ul>
</div>

Open in new window

0
Comment
Question by:niceoneishere
  • 5
  • 5
11 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 40323386
jQuery(function($) { // similar do document.ready

$("img", each(function() {
        var oldSrc = $(this).attr("src");
        var newTwoLetters = "AZ";
        var newSrc = oldSrc.replace(/^([^-]*-)..(\.jpg)$/,"$1" + newtwoLetter + "$2");
        $(this).attr("src", newSrc);
});

});

Open in new window

0
 
LVL 2

Author Comment

by:niceoneishere
ID: 40323407
Thanks for replying sir one question how Should I reference this to apply only to the <a> tags href that are in the ul#thumbnails1
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40323414
$("img" -> $("#thumbnails1 a"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:niceoneishere
ID: 40323544
Thanks I added this to my product page but its not working. Please ignore the colorbox code

	 <script type="text/javascript">
	 $(document).ready(function(){
		$('.colorbox').colorbox({
			overlayClose: true,
			opacity: 0.8,
			maxHeight: 550,
			maxWidth: 550,
			width:'100%'
		});
	
       $("#thumbnails1 a", each(function() {
        var oldSrc = $(this).attr("src");
        var newTwoLetters = "AZ";
        var newSrc = oldSrc.replace(/^([^-]*-)..(\.jpg)$/,"$1" + newtwoLetter + "$2");
        $(this).attr("src", newSrc);
       });		
	 });
	</script>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 40323564
Replace "src" by "href"
0
 
LVL 2

Author Comment

by:niceoneishere
ID: 40323582
Sadly its still not working, anyway I can send you my website link in an email so you can see it

Thanks
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40323618
Here a test page with the code fixed : http://jsfiddle.net/a1ysw899/

$("#thumbnails1 a").each(function() {
        var oldSrc = $(this).attr("href");
        var newTwoLetters = "AZ";
        var newSrc = oldSrc.replace(/^(.*)..(\.jpg)$/,"$1" + newTwoLetters + "$2");
        $(this).attr("href", newSrc);
});

Open in new window

0
 
LVL 2

Author Comment

by:niceoneishere
ID: 40323716
Ok I made the change as you gave now it replaces all the <a tag last 2 letters before .jpg to AZ

So I replaced the var newTwoLetters = "Lg" and it works.  Is that correct
0
 
LVL 83

Expert Comment

by:leakim971
ID: 40323786
yes, if you want to check, use the test page
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 40325171
Currently the change will change all xx.jpg to Lg.jpg
If you need to change
sm to lg
SM to LG
Sm to Lg
you need to do something like

 
var letters = oldSrc.substr(oldSrc.indexOf(".jpg")-2,2);
var newSrc = oldSrc.replace(letters+".jpg",{"sm":"lg","SM":"LG","Sm":"Lg"}[letters]+".jpg");

Open in new window

0
 
LVL 2

Author Closing Comment

by:niceoneishere
ID: 40360817
Thanks and appreciate it
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question