I have an image path that is being outputted to the href of an anchor tag. I'd like to set that image as the background for the anchor.
This is my snippet:
jQuery('.initialize a').css({
backgroundImage: 'url(' + this.href + ')'
,backgroundRepeat: 'no-repeat'
,backgroundPosition: '0 0'
});
Which returns background-image: url(foo/undefined); on the anchor.
What am I doing wrong?
ETA:
jQuery('.initialize a').each(function() {
this.setAttribute("href", this.getAttribute("href").replace(',\'', ''));
var href = this.getAttribute("href");
this.setAttribute("href", href.substr(0, href.length-14));
});
So I'm using the above to clean the output for the href attribute up a bit.
The suggested solution to my problem:
$(this).css({background-image: 'url(' + this.href + ')', ...});
should fit right in there. But I am not getting this to work, presumably due to the way my jQuery function is formatted. The $ is unavailable to me a at present. How can I fit this in?