Using Variables in JQuery Selectors

If you want to use variables in combination with CSS-style selectors like so:

$("#navigation a.myHomePage, " + logo).hover(function() {
$("li.home").addClass("homeRoll");
}, (function() {
$("li.home").removeClass("homeRoll");
});

Make sure your variable homePageImage is written as a string:

var homePageImage = "#logo"

not an object:

var homePageImage = $("#logo")

or it won’t work.

This entry was posted in jQuery and tagged , . Bookmark the permalink.

2 Responses to Using Variables in JQuery Selectors

  1. Polprav says:

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. admin says:

    Absolutely! As long as I didn’t say anything incriminating :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>