Solved

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

Posted on 2015-02-03
2
61 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

691 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