views:

723

answers:

3

So I am attempting to combine two image galleries, Galleria and Lightbox. The way it is supposed to work is that Lightbox will read the href from the anchor tag and Galleria will read the src from the image tag in the following code:

<a href="./public/images/gallery/2.JPG">
<img class="replaced" src="./public/images/gallery/2.JPG"/>
</a>

I am attempting to pull the attribute value of the src out and use it as the value of the href. Just for now, once I get this bit working I have to re-work the code.

This is how I am pulling the attribute value out

var imageSrc = image.attr('src');

Right now I have tested it in FF2,FF3 and neither of them are reading the value. In Chrome and IE it reads the value. What I was thinking is that the image is created in jQuery so perhaps Firefox is moving too quickly reading through and the jQuery hasn't had time to create the image yet?

Any thoughts?

Thanks,
Levi

A: 

Well how are you selecting image?

Try:

$('.replaced').each(function(){
   var imageSrc = $(this).attr('src');
   //alert(imageSrc) /*  for IE  */
   console.log(imageSrc);
});
Dmitri Farkov
A: 

Check the following:

  • Did you define image correctly? How are you assigning a value to this?
  • Did you ask for the value of image after the position of the DOM element representing that image?
  • Did you make sure to put your scripts at the end of your body, just before the closing </body> tag? (If you don't, it's possible you'll wind up with a null reference and any Javascript execution will terminate.)

Chances are good that it's related to one of these three issues.

John Feminella
Why would he need to put his script at the end of the body?! That's what the ready event is for
Nadia Alramli
image is a parameter of a function, it refers to this line where it is set:var _img = $(new Image()).attr('src',_src).addClass('replaced');I am creating the image in one JS file and then in the JS file following it I assign create and assign the value of the anchor tag. It seems to be in the correct order, I mean it does work in IE and Chrome, does FF have a different order for doing things?
Levi
This is the url of what I am working on, I have firebug open but I am new to it and don't know how to use it fully. http://www.lewiscreekbuilders.com/test/?gallery-with-testimonials/gallery/?galId=62#./public/images/gallery/2.JPG
Levi
@Nadia: I don't know if he using the ready event or not. But doing so removes a possible source of errors.
John Feminella
A: 

Are you wrapping your call in a ready event?

$(document).ready(function(){
    var imageSrc = $('img.replaced').attr('src');
});

Please note that if you have multiple images that share the class 'replaced' you need to read the src from each one of them. The above method will only work on a single image, if you want to read the attribute for multiple images use this:

$(document).ready(function(){
    var imageSrc = $('img.replaced').each(function(){
        $(this).attr('src');
    });
});
Nadia Alramli
Yes it is wrapped, and there is only one instance of an image with a class of 'replaced'. You can see what I am working on at this url http://www.lewiscreekbuilders.com/test/?gallery-with-testimonials/gallery/?galId=62#./public/images/gallery/2.JPG
Levi