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-20-2011, 01:54 AM   PM User | #1
TomCat
New Coder

 
Join Date: May 2009
Location: USA
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
TomCat is an unknown quantity at this point
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"
TomCat is offline   Reply With Quote
Old 02-20-2011, 05:50 AM   PM User | #2
oVTech
Regular Coder

 
oVTech's Avatar
 
Join Date: Nov 2010
Location: USA
Posts: 296
Thanks: 4
Thanked 54 Times in 52 Posts
oVTech is an unknown quantity at this point
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-



oVTech is offline   Reply With Quote
Users who have thanked oVTech for this post:
TomCat (02-20-2011)
Old 02-20-2011, 04:31 PM   PM User | #3
TomCat
New Coder

 
Join Date: May 2009
Location: USA
Posts: 23
Thanks: 6
Thanked 0 Times in 0 Posts
TomCat is an unknown quantity at this point
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.
TomCat is offline   Reply With Quote
Reply

Bookmarks

Tags
addclass, jquery, live, simple

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 05:56 PM.


Advertisement
Log in to turn off these ads.