Solved

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

Posted on 2014-09-15
11
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 82

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 1

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 82

Expert Comment

by:leakim971
ID: 40323414
$("img" -> $("#thumbnails1 a"
0
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!

 
LVL 1

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 82

Expert Comment

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

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 82

Accepted Solution

by:
leakim971 earned 500 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 1

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 82

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 1

Author Closing Comment

by:niceoneishere
ID: 40360817
Thanks and appreciate it
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 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