Solved

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

Posted on 2014-09-15
11
227 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I have a few questions about using the Google Developer's Console. 4 42
jquery tab header text 1 23
Html value of radio 14 31
Wordpress Only run code if on a certain page 11 23
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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 …
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…
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…

830 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