Solved

How can I find and replace part of a url based on element id contains?

Posted on 2015-02-03
2
56 Views
Last Modified: 2015-02-14
My page is a grid of products with several images.  The images each of unique id's that share a common prefix "thumb_".  I need to find each image based on each image whose id contains "thumb_" and replace the src from "_md" to "_lg".

Example:
<img id="thumb_1234_abc" src="http://www.mystore.com/images/product/1234_abc_md.jpg" >
<img id="thumb_5678_abc" src="http://www.mystore.com/images/product/5678_abc_md.jpg" >

Updated to:
<img id="thumb_1234_abc" src="http://www.mystore.com/images/product/1234_abc_lg.jpg" >
<img id="thumb_5678_abc" src="http://www.mystore.com/images/product/5678_abc_lg.jpg" >
0
Comment
Question by:JJ2357
2 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 500 total points
ID: 40586950
you can try something like this using jquery:

var source = '';
$('input[id*="thumb_"]').each() {
     source = $(this).attr("src").replace(("md", "lg" );
    $(this).attr("src", source);
});

Open in new window

0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40587839
Might I recomend changing the above replace from "md", "lg" to "md.jpg", "lg.jpg". It seems like you are really trying to change the end of the url. This way you can avoid messing up and image that has the letters md anywhere else in the url.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now