Solved

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

Posted on 2015-02-03
2
60 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
[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
2 Comments
 
LVL 33

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email validation 9 49
Hide and Unhide Table 6 26
15 puzzle in Javascript/html 2 43
Execute jQuery after Function 4 10
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

749 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