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 10-04-2012, 08:58 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
How can I add multiple listeners to an element with jQuery

Hello,

I am having some problems with adding multiple listeners to this <a> element. As you can see, there are 3 listeners I would like to apply.

Code:
<a class="glossaryLink" href="#" onmouseover="onRollOverPULink(event, 'Life');" onmouseout="onRollOutPULink(event);" onclick="defineTerm(event);">Life</a>
I was thinking that I would use the class to target multiple <a> tags, so I would only need this: (would I still need the href?)
Code:
<a class="glossaryLink" href="#">Life</a>
But from here I can't seem to get 3 listeners to work properly.
m2244 is offline   Reply With Quote
Old 10-04-2012, 09:03 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
You can chain them, like so. I think this is what you are looking for

Code:
$('a.glossaryLink').mouseover(function(event){
onRollOverPULink(event, 'Life')
}).onmouseout(function(event){
onRollOutPULink(event)
}).click(function(event){
defineTerm(event)
})
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 10-04-2012, 09:54 PM   PM User | #3
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Still can't get it. I can't even get this one function to work.

Code:
<ul>
  <li>
    	(U) Spiral 1.5: <a class="glossaryLink" href="#">SCCVI</a> and <a class="glossaryLink" href="#">SCRI</a>  </li>
  <li> (U) Spiral 2:  <a class="glossaryLink" href="#">HBSS</a>  </li>
</ul>
Code:
function setGlossLinkListeners()
{
	$('a.glossaryLink').hover(function(){
		onRollOverPULink()
	})
}
m2244 is offline   Reply With Quote
Old 10-05-2012, 01:36 AM   PM User | #4
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
you dont need to contain them inside of a function like that ( unless you only want to activate them on demand for some reason) , put this in a script block in the head section and give it a try


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

$('a.glossaryLink').mouseover(function(event){
var linkTxt = $(this).text();
onRollOverPULink(event, linkTxt)
}).onmouseout(function(event){
onRollOutPULink(event)
}).click(function(event){
defineTerm(event)
})

})
this will bind the lsiteners as soon as the dom is ready. if you need will be adding and removing elements on the fly, then you will need to go a step further and use .on()
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 10-05-2012 at 01:43 AM..
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
m2244 (10-05-2012)
Old 10-05-2012, 12:47 PM   PM User | #5
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Quote:
Originally Posted by DanInMa View Post
you dont need to contain them inside of a function like that ( unless you only want to activate them on demand for some reason) , put this in a script block in the head section and give it a try

this will bind the lsiteners as soon as the dom is ready. if you need will be adding and removing elements on the fly, then you will need to go a step further and use .on()
Thanks for the help here.

The content on the page is dynamic, it is loaded every time the user clicks the 'next' button, so this needs to run each time that happens.

I tried to simplify this so that only one function is added, in an attempt to get something working here. Still nothing. Does it have something to do with the document not being ready?

I was just messing around with .on() but I get an error telling me that it is not a function. Does that mean that I have an old version of jQuery?

Last edited by m2244; 10-05-2012 at 01:00 PM..
m2244 is offline   Reply With Quote
Old 10-05-2012, 01:14 PM   PM User | #6
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
thats quite possible, the older way is .live()
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 10-05-2012, 01:27 PM   PM User | #7
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,608
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by m2244 View Post
I was just messing around with .on() but I get an error telling me that it is not a function. Does that mean that I have an old version of jQuery?
That could very well be. And it is also easy to find out by looking at the file name of the jQuery file or, if that doesn’t indicate it, right into the jQuery file where the version usually is displayed prominently. You can then compare it with the version number shown on http://jquery.com – and get the latest version right away if applicable.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 10-05-2012, 01:38 PM   PM User | #8
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
Quote:
Originally Posted by VIPStephan View Post
That could very well be. And it is also easy to find out by looking at the file name of the jQuery file or, if that doesn’t indicate it, right into the jQuery file where the version usually is displayed prominently. You can then compare it with the version number shown on http://jquery.com – and get the latest version right away if applicable.
Great advice, thanks.

By the way, I have NOT clicked your link.

This is what I ended up with:

Code:
function setGlossLinkListeners()
{
	$('a.glossaryLink').live("click", function(event){
		onRollOverPULink();
	});
}
But now I have another problem. When the link is clicked, it seems that the function onRollOverPULink() is running multiple times.

Last edited by m2244; 10-05-2012 at 01:46 PM..
m2244 is offline   Reply With Quote
Reply

Bookmarks

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 02:44 PM.


Advertisement
Log in to turn off these ads.