tags:

views:

57

answers:

3

I am trying to get jquery to pick up variable that I am defining and I'm having issues doing it.

$.ajax({
url: "sitemap.php",
cache: false,
success: function(html){
    $("#results").append(html);
        var $seriestitle = $("#define-title").text();
    $('#results a:contains("$seriestitle")').addClass('current-series');
    $('a:not(".current-series")').hide();
}
});

This is the code I am using. You can view the page at the following URL:

http://benjammindesigns.com/XML/details/1231.html

There is a span on the page, with the ID 'define-title', that contains text. I am trying to pull the text from that span and use it as my variable.

Any info is greatly appreciated.

+3  A: 

you want this:

var $seriestitle = $("#define-title").text();
$('#results a:contains("'+$seriestitle+'")').addClass('current-series');
mkoryak
Awesome, works perfectly. Thanks, mkoryakJust to further my understanding, what causes the need for the 'plus signs' and extra quotes on either side?
Batfan
@Batfan: JavaScript does not do variable interpolation the way PHP does. Do not let the optional `$` in the variable name fool you.
Tomalak
@TomalakLayman's terms, please :)
Batfan
@Batfan: I suspected you come from a PHP background. In PHP you actually can do a thing similar to what you have tried. It's called "variable interpolation". If you *don't* come from a PHP background, never mind. ;)
Tomalak
@Tomalak: Thanks you for explaining :) I DO have some PHP background but it is very limited. Does it require the extra quotes and plus signs because it is pulling a variable?
Batfan
@Batfan: If you want to put it like that. It builds a dynamic string from the fixed parts + the variable content.
Tomalak
+1  A: 
$('#results a:contains("'+$seriestitle+'")').addClass('current-series');
Adam Kiss
+1  A: 

The span you're trying to use is in the head of your document. Move it to the body.

Diodeus