0

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.

1
  • 1
    Actually, the $ prefix is (by convention) reserved for variables that contain jQuery objects. It should be var seriestitle = $("#define-title").text();, since text() does not return a jQuery object. Commented Apr 12, 2010 at 20:51

3 Answers 3

3

you want this:

var $seriestitle = $("#define-title").text();
$('#results a:contains("'+$seriestitle+'")').addClass('current-series');
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome, works perfectly. Thanks, mkoryak Just to further my understanding, what causes the need for the 'plus signs' and extra quotes on either side?
@Batfan: JavaScript does not do variable interpolation the way PHP does. Do not let the optional $ in the variable name fool you.
@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: 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: If you want to put it like that. It builds a dynamic string from the fixed parts + the variable content.
1
$('#results a:contains("'+$seriestitle+'")').addClass('current-series');

Comments

1

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

Comments

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.