Connecting Tech Pros Worldwide Forums | Help | Site Map

Event listener doesn't wait for click

Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#1: Oct 16 '08
Hi Guys,

I'm trying to set an eventlistener for a bunch of DIV tags. can you tell me why this code wont work?

Expand|Select|Wrap|Line Numbers
  1. display=document.getElementById("display");
  2. field=display.getElementsByTagName('div'); 
  3.  
  4. for (var i in field){         
  5.    if (field[i].addEventListener)
  6. field[i].addEventListener('click',go(),false);
  7. else if (field[i].attachEvent)
  8. {           
  9. field[i].attachEvent('onclick',go());         
  10. }
right now as soon as the page loads it triggers go() (which is a page redirect function)

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Oct 16 '08

re: Event listener doesn't wait for click


You are telling it to do so-
replace go() with go

The parens call the function immediately
Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#3: Oct 17 '08

re: Event listener doesn't wait for click


Quote:

Originally Posted by mrhoo

You are telling it to do so-
replace go() with go

The parens call the function immediately

Thanks, but now I'm in a worse situation than I started. the script just locks now. it won't do anything.

EDIT: realised that when the code was run, the element it's looking for doesn't exist, moved it, now the page loads, but still no event...
Member
 
Join Date: Oct 2008
Location: USA
Posts: 55
#4: Oct 17 '08

re: Event listener doesn't wait for click


what does your 'go' function look like? I try not to use event listeners... they are a very useful function of javascript, but with a little bit of work, you can duplicate the functionality of an event listener without the cross browser problems:)

another thought is to use a "bind closure" function (I prefer to call it bind method, but bind closure is the industry standard name)

Expand|Select|Wrap|Line Numbers
  1. function bindMethod(func,args){
  2.     var tmp=args
  3.     return func(tmp)
  4. }
  5.  
  6. addEventListener('click',bindMethod(go,myArguments),false)
  7.  
this will bind the arguments that you need sent to the function to your event regardless of scope.

also....if this is an form field, why don't you use the focus event? a click event is the combination of a mousedown and mouseup event....
Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#5: Oct 17 '08

re: Event listener doesn't wait for click


Quote:

Originally Posted by zaphod42

what does your 'go' function look like? I try not to use event listeners... they are a very useful function of javascript, but with a little bit of work, you can duplicate the functionality of an event listener without the cross browser problems:)

another thought is to use a "bind closure" function (I prefer to call it bind method, but bind closure is the industry standard name)

Expand|Select|Wrap|Line Numbers
  1. function bindMethod(func,args){
  2.     var tmp=args
  3.     return func(tmp)
  4. }
  5.  
  6. addEventListener('click',bindMethod(go,myArguments),false)
  7.  
this will bind the arguments that you need sent to the function to your event regardless of scope.

also....if this is an form field, why don't you use the focus event? a click event is the combination of a mousedown and mouseup event....

Go is literally just a "window.location" call, I'm trying to creating dynamic hyperlinks from the content of an ajax call. these are not form fields, they're textual elements.
Member
 
Join Date: Oct 2008
Location: USA
Posts: 55
#6: Oct 17 '08

re: Event listener doesn't wait for click


so you need to call a function that opens a window or changes the url of the current window based on the content of a text element on the page?

do you have any script you could post?
Tarantulus's Avatar
Member
 
Join Date: May 2007
Posts: 103
#7: Oct 17 '08

re: Event listener doesn't wait for click


Quote:

Originally Posted by zaphod42

so you need to call a function that opens a window or changes the url of the current window based on the content of a text element on the page?

do you have any script you could post?

yep pretty much hit the nail on the head. to clarify:

$object chosen from list
ajax pulls back $data from PHP using id of $object
$data comes as <div id=key>alphanumeric<key><div id=value>$numeric </value>
here is the bit I can't do, create an event listener so that div "value" is a link to http://url.com/index.php?$numeric

then an AJAX SQL call is made to select name where id =$numeric.

hope that makes more sense.

here is the actual code:

[HTML]
<div id="display">
<div class="key" id="key-resource" ></div>
<div class="value" id="label-resource">9520</div>
</div>
[/HTML]

and the javascript is as in the 1st post.
Member
 
Join Date: Oct 2008
Location: USA
Posts: 55
#8: Oct 17 '08

re: Event listener doesn't wait for click


okay, I don't know where your "go" function is coming from, or what arguments you could be sending it based on how you seem to be setting this up....but window.location is only part of that. you need to use location.href, there is also a location.hash property. To make the div into link however, you might be better off not setting an event listener.....the way I understand them event listeners will stack, e.g. if you keep adding them and the page doesn't reload, which call to "go" do you want to fire? better to just set the mouseup of the div in the javascript directly:

Expand|Select|Wrap|Line Numbers
  1. var myUrl='http://someplace.else'
  2. myDiv.onmouseup=function(){ go(myUrl) }
  3.  
  4. go=function(url,hash){
  5.     window.location.href=url
  6.     window.location.hash=hash || ''
  7. }
for the current window or just:

Expand|Select|Wrap|Line Numbers
  1. go=function(url){
  2.     window.open(url)
  3. }
the following link has some really good info on the location property:

DevGuru : Location
Reply


Similar JavaScript / Ajax / DHTML bytes