Solved

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

Posted on 2014-09-15
11
224 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now