CSS and JQuery: spaces inside image name break code of url()

Posted by Shyam on Stack Overflow See other posts from Stack Overflow or by Shyam
Published on 2010-05-18T10:17:59Z Indexed on 2010/05/18 10:30 UTC
Read the original article Hit count: 120

Filed under:
|
|
|

Hi,

I have a page that is supposed to display a larger version of an image when hovered over a thumbnail.

I have a 'div' with an ID and the JQuery code is as following:

$(document).ready(function(){

  $('img').hover(function() {

    var src = $("#im" + this.id).attr("src");
    $('#viewlarge').css('backgroundImage','url(' + src +')'); 
    return false;
  });

});

The images I use, are generated by a Ruby script that "generate" an image with a similar, yet different id. However, sometimes, photo's are uploaded that have "spaces" inside. My developer tools tell me that the background-image is not set correctly, yet the image path is correct and the browser don't have problems finding the image.

My question is, can I somehow sanitize the url 'src', so spaces won't be a problem? I am aware of doing this server side, yet I would like to know how to do this with JQuery/JS too.

Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript