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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    USA
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Using addClass() function with live()

    I am trying to use the .live() jQuery function in cooperation with the .addClass() function, but I am unsure of how exactly to pass the parameter of the class-to-be-added's name through the live() function to work with addClass().

    Here is an example of the jQuery I would be using:

    Code:
    $('.mnav').live('click', function() {
    	$(this).addClass('mnav-over').removeClass('mnav');
    	$('.mnav-over').addClass('mnav').removeClass('mnav-over');
    });
    I would like to use addClass() in both instances in the code above in cooperation with live() while being able to pass the class's name through the live() function. I've searched around the web quite a bit, but none of my searches have resulted in an answer that fits my specific needs.

    I appreciate in advance any help that I may receive.
    Last edited by TomCat; 02-20-2011 at 04:33 PM. Reason: Changed topic prefix to "Resolved"

  • #2
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    You could do something like this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    .mnav {
        background-color: #f00;
    }
      
    .mnav-over {
        background-color: yellow;
    }
    </style>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
       
        $('#xyz').live('click', function(){
        	   $(this).toggleClass('mnav-over'); 
         });
    
    });
    </script>
    </head>
    <body>
    
       <p class="mnav" id="xyz">Click me!</p>
    
    </body>
    </html>




    I don't know, I don't care, and it doesn't make any difference!
    -Albert Einstein-




  • Users who have thanked oVTech for this post:

    TomCat (02-20-2011)

  • #3
    New Coder
    Join Date
    May 2009
    Location
    USA
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I apologize, but after reevaluating my logic I realized that using click() with live(), as I was already doing, was enough to make addClass() operate as though it was being used with live() itself. The problem arose in my specific case where the CSS background color was not changing due to an external influence.

    I was able to resolve the problem without needing an answer to this one. Thank you for your help, though, oVTech.


  •  

    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
    •