I am trying to test for an attribute in a dynamic php photo gallery that loads images from a folder into a gallery. The gallery uses lightbox to display the photo once clicked. This is what I’m using to test:
if( $('#lightboxImage').attr('src') !== 'undefined'){ do something here }
I’m getting an initial error (Cannot call method ‘attr’ of null) in debug I think because the #lightboxImage only has a ‘src’ attribute when an image from the gallery is clicked. Nothing is obviously clicked when the page first loads.
Any ideas on how to solve this?
Thanks
I’m with @Malvolio, your error doesn’t quite add up. You should not be getting the error you’re describing. To wit:
http://jsfiddle.net/7ZGwe/
Running
onloadgives me:So your actual problem has to do with something else, as Malvolio suggests. I can’t necessarily speculate any better than what Malvolio has, except maybe
jQuery()should be tried in place of$().This is a little bit beside your underlying problem, but has to do with what you asked. Instead of doing a test, you could:
http://jsfiddle.net/jSQ3P/3
In your selector and bypass the check. So in other words: