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 11-03-2012, 03:57 PM   PM User | #1
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
jquery .click() issue

hi.
i'm having hard time with binding events in dynamically loaded elements...
i wrote this function:
Code:
function bindLinks(){
	console.log('binding');
	$('#ajaxDiv > a').click(function(ev){
		ev.preventDefault();
		otherFunction();
	});
}
and now. i'm loading contents with number of links to 'ajaxDiv', then bindLinks() function is triggered (and it works).
but when i empty contents of 'ajaxDiv', load new stuff and bindLinks() is called again, events are not being assigned.
i know function is called since i get console output every time.
any idea how to fix it?
patryk is offline   Reply With Quote
Old 11-03-2012, 04:25 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,587
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Try on('click', function(ev) {…})
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
patryk (11-03-2012)
Old 11-03-2012, 05:00 PM   PM User | #3
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
thanks
if someone will look for that, here's working one (with 'parent > child' selection):
Code:
function bindLinks(){
	$('#ajaxDiv').on('click', 'a', function(ev){
		ev.preventDefault();
		otherFunction();
	});
}
patryk is offline   Reply With Quote
Old 11-03-2012, 05:15 PM   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
if you are calling this function mutiple times on the same page , you dont need to. If you put the .on function in in a dom ready block it will do it's thin automatically even if you add or remove anchor links from the div dynamically. lik so:

Code:
$(function(){	
  $('#ajaxDiv').on('click', 'a', function(ev){
		ev.preventDefault();
		otherFunction();
	});
});
__________________
- 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
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 06:36 PM.


Advertisement
Log in to turn off these ads.