Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2011, 04:04 PM   PM User | #1
mr curious
New Coder

 
Join Date: Jan 2011
Posts: 55
Thanks: 7
Thanked 0 Times in 0 Posts
mr curious is an unknown quantity at this point
jquery variables for functions

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:

Code:
$(document).ready(function(){ 

$('#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
this:

Code:
$('\'#' + $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.

Thanks
mr curious is offline   Reply With Quote
Old 02-24-2011, 04:17 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
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
PHP Code:
$(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
PHP Code:
$('#' $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.
venegal is offline   Reply With Quote
Reply

Bookmarks

Tags
click function, jquery, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:00 AM.


Advertisement
Log in to turn off these ads.