Solved

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

Posted on 2014-09-15
11
225 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 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now