View Full Version : jquery variables for functions

mr curious
02-24-2011, 05:04 PM
I'm creating a click function for a set of links that changes the background image of each of those links( different images for each link).

What I have is this:


$('#nav a').click(function() {

//$('#hlink').css({ backgroundImage : "url(images/home_animate.jpg)" });


where #nav is the id for the div containing the links and #link being the id of one of the links.

this all works fine, but what I want is

$('\'#' + $element + '\'').css({ backgroundImage : "url(images/home_animate.jpg)" });

where $element stores the id of the link clicked.
I can get $element to store the id of the link clicked but when I try to append
$element to the .css({ backgroundImage : "url(images/home_animate.jpg)" }); it doesn;t work
any help would be great.


02-24-2011, 05:17 PM
If this is about doing something with same element that triggered the event (the link that has just been clicked): That one's stored in this, so

$(this).css({ backgroundImage : "url(images/home_animate.jpg)" });
will work just fine.

But if you really have to use the id stored in $element, the correct syntax would be

$('#' + $element).css({ backgroundImage : "url(images/home_animate.jpg)" });

$element, btw, is an odd variable name for a string. Usually, variables that start with '$' are used only for jQuery objects.