Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •