views:

64

answers:

3

I have the following markup with inline javascript and would like to change it to Jquery. Any help would be appreciated.

<a title="823557" href="/PhotoGallery.asp?ProductCode=471823557" id="product_photo_zoom_url">
<img border="0" onload="vZoom.add(this, '/v/vspfiles/photos/471823557-2.jpg');"
alt="823557"
src="/v/vspfiles/photos/471823557-2T.jpg" id="product_photo"></a>

I guess I would need to use this?

$(function(){
<---- somecode---->
});
+5  A: 
$(function () {
   $("#product_photo").load(function (e) {
      vZoom.add(this, this.src.replace('T.', '.'));
   })
})();

If $ doesn't work for some reason, this should also work. I incorporated Kranu's advice since that library most likely only needs the DOM loaded as a prerequisite, rather than the load event:

jQuery(function ($) {
    $("#product_photo").each(function () { // in case there is more than one 
        vZoom.add(this, this.src.replace('T.', '.'));
    });
});
CD Sanchez
unless it's a typo, the img src is different from the second parameter to the .add function
davidsleeps
also need to string replace the `T.jpg` with `.jpg` in `this.src`
MattSmith
@davidsleeps: It's the same `src` - just with a T added at the end (I assume to show that it's a thumbnail. Notice it's a modified value of `this.src`. @MattSmith: It already does that.
CD Sanchez
This is exactly what I was looking for, will test but it look like it will work!!!
Getting a javscript error "not a function"
@user357034: Does it tell you what is not a function? `$` is not a function?
CD Sanchez
@Daniel, your right, I posted my comment before the answer was edited...
davidsleeps
The second code worked perfectly, couldn't get the first one to work though. Really don't need the each function but it didn't hurt.
@user357034: Yes, you probably have a conflict with another library or something. In the future, put any jQuery code that is dependent on the DOM in that function.
CD Sanchez
I do not have any other lib loaded, I changed the jquery(function($){ to $(function($){ with no problems. thanks for the help!!!
+1  A: 

Note that there is no need to put a separate bind event on the img because the $(function() { }) waits until the body loads.

$(function(){
    vZoom.add(document.getElementById('product_photo'),'/v/vspfiles/photos/471823557-2.jpg');
});
Kranu
This is kinda what i want except instead of the static link to the image I need to do a string replace as in the example from @Daniel from the src to the following xxxxxxx-2.jpg
Well I'm glad you at least acknowledged my answer. I do admit that the str_replace thing is good for portability.
Kranu
A: 

Not exactly sure what you are trying to do, but essentially you would remove the image from the HTML and dynamically load it using JS. Once loaded, you would inject it in the DOM and set the onload event.

var jsImg = new Image();
jsImg.onload = function(){
  vZoom.add(this,'/v/vspfiles/photos/471823557-2.jpg');
  var img = document.createElement('IMG');
  img.setAttribute('id','product_photo');
  img.setAttribute('alt','823557');
  img.setAttribute('src','/v/vspfiles/photos/471823557-2T.jpg');
  img.style.border = 'none';
  //inject image now in the DOM whereever you want.
};
jsImg.src = '/v/vspfiles/photos/471823557-2T.jpg';
Rajat