0

Hey guys I'm trying to add the string 'url(" ") ' around my a href 'rel' attribute after calling it through jQuery. I just don't know the right syntax. Here is what I have. I tried to put the url("") inside the rel tag, and that didn't work.

Any help would be greatly appreciated thanks.

$(function() {
    $('#list li a').hover( function(){
        $("#meow").css( "background", $(this).attr('rel') );
    }); 
});
1
  • 1
    What you want to do is called string concatenation. Commented Jan 27, 2012 at 2:43

2 Answers 2

2

Like this (thanks @nnnnnn):

var thisRel = $(this).attr('rel'),
theURL = 'url("' + thisRel + '")';
$("#meow").css( "background", theURL );
Sign up to request clarification or add additional context in comments.

4 Comments

That's not quite right: it assumes there is already a variable called rel, and then tries to retrieve an attribute with a name that is the result of the 'url("' + rel + '")' concatenation - I believe the intention is to retrieve the 'rel' attribute first and concatenate the result with the 'url("' stuff.
Thanks :) , But now I get an error "rel is not defined", any insight?
try this again, I slightly misunderstood your question and @nnnnnn caught it. This has been edited to set two vars first.
I see this is your first post, so as a reminder - if you are satisfied with an answer please 'check' it. Thanks
0

If you are saying that for an example anchor element like this:

<a href="http://something.com" rel="http://whatever.com">Test</a>

You want to extract the rel attribute and wrap it in 'url("...")' so that you end up with this string:

'url("http://whatever.com")'

Then you can do that as follows:

var newString = 'url("' + $(this).attr("rel") + '")';

(Obvioulsy that assumes that this is the anchor element in question.)

To put that together with the code from your question:

$(function() {
   $('#list li a').hover( function(){
       $("#meow").css( "background", 'url("' + $(this).attr('rel') + '")' );
   });
});

2 Comments

Thanks nnnnnn, but for some reason my rel attribute keeps coming back undefined? Do I need to preload them and if I did, how would I go about that?
actually scratch what I said your solution works dandy, a thousand thank you's :)!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.