tags:

views:

74

answers:

4

Hi. I have a bunch of images on a page that are contained within a div with a class of content-block like this:

<div class="content-block">
    <img src="../images/path/path/image.jpg" alt="blah" title="blah" />
    <img src="../images/path/path/image.jpg" alt="blah" title="blah" />
    <img src="../images/path/path/image.jpg" alt="blah" title="blah" />
    <img src="../images/path/path/image.jpg" alt="blah" title="blah" />
    <img src="../images/path/path/image.jpg" alt="blah" title="blah" />
</div>

I am loading this html into a div via ajax. When I load it in I need to remove the "../" before the image path, after it loads in so the path remains correct. Can this be done with JQuery? Many thanks in advance.

MY JQUERY CODE FOR LOADING IN THE HTML:

$(document).ready(function() {
$('.projects a').click(function(event) {
    $('#work').load(this.href + ' #loadwork');
    event.preventDefault();
});

});

EDIT

$(document).ready(function() {
$('.projects a').click(function(event) {
    $('#work').load(this.href + ' #loadwork', function(){
           $('.content-block>img').each(function(){
           $(this).attr('src',$(this).attr('src').replace('../',''));
        });
    });
    event.preventDefault();
});

});

+2  A: 
$('.content-block>img').each(function(){
  $(this).attr('src',$(this).attr('src').replace('../',''));
});

That should do the trick.

--edit--

based on your code, try it like this:

$(document).ready(function() {
  $('.projects a').click(function(event) {
    $('#work').load(this.href + ' #loadwork',function(response){
      $(response).find('div>img').each(function(){
        $(this).attr('src',$(this).attr('src').replace('../',''));
        // Output the response to the DOM
      });
    });
    event.preventDefault();
  });
});

I think that should work... you might have to play a little bit with the find selector.

Brant
@Brant this is not working. Could it be something to do with the fact I am loading the html in via ajax? I have editted my post above to show the jquery I am using
mtwallet
The code itself will work, it's a matter of putting it in the right spot. You can do it in a couple of ways... either translate the ajax response into a jQuery object and then do the replacing or output it all to the page, then use the replace as a callback.
Brant
I added a second section based on the code you added.
Brant
Wierd your latest codes doesn't work but the previous suggestion works a treat. I have edited my post so you can see.
mtwallet
There very well may be a problem with the .find selector in there... I wasn't at a computer with the ability to test before posting.
Brant
A: 

Try this (not tested):

$('.content-block img').each(function() {
        var path = $(this).attr('src');
        path = path.substr(3);
        $(this).attr('src', path);
    }
);
Sorcy
+1  A: 

You probably want to execute the replace in a callback from the load method so that it only executes after your images have been loaded. Something like this (using Brant's replace code):

$('#work').load(this.href + ' #loadwork', function()
{
   $('.content-block>img').each(function(){
   $(this).attr('src',$(this).attr('src').replace('../',''));
});
}));
Dan Diplo
@Dan thanks for the help that did the trick
mtwallet
Well, Brant did half the work so credit to him too.
Dan Diplo
A: 

Or, alternatively, generate the HTML via a server side method that calculates the URLs relative to the root of the application ("/appName/images/path/path/image.jpg"), and then you can load the markup into any page without worrying about whether it will work or not. This prevents you from having to process the html fragments on the client side, making your pages a bit cleaner.

belugabob
@belugabob Thanks for the response. I would not know where to start with a solution like this! but it does sound like a better overall solution as I do like to keep my code as clean as possible. Can you point me to some further reading/tutorials on this?
mtwallet
In the server side code that produces the markup, you should be able to do this...<img runat="server" src="~/images/path/path/image.jpg" alt="blah" title="blah" />
belugabob
You should be aware, however, that the URLs being generated are relatrive to the URL of the AJAX call, which may not be at the same directory level as the page into which the markup is being posted. If this is the case, the URLs will be wrong for the host page. This whole subject is fraught with problems and should be approached carefully.
belugabob