473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Event listener doesn't wait for click

Tarantulus
114 100+
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)
Oct 16 '08 #1
7 4723
mrhoo
428 256MB
You are telling it to do so-
replace go() with go

The parens call the function immediately
Oct 16 '08 #2
Tarantulus
114 100+
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...
Oct 17 '08 #3
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....
Oct 17 '08 #4
Tarantulus
114 100+
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.
Oct 17 '08 #5
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?
Oct 17 '08 #6
Tarantulus
114 100+
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.
Oct 17 '08 #7
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
Oct 17 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Amir Hardon | last post by:
I am dynamically adding rows to a table, and each row have a button which removes it. I have successfully implemented this for mozilla but I'm having troubles with IE, here is how I did it: ...
4
by: Newbie | last post by:
Is it possible to set up an event handler or something else so that when *any* link on the page is clicked it 'fires-up', executes some JS and then continues to process the link that was clicked?...
2
by: kj | last post by:
How does one trigger an event programmatically? I'm interested in how to do this in both the "Level 0" event model as well as in the DOM Level 2 event model. Thanks! kj -- NOTE: In my...
17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
0
by: Mats-Lennart Hansson | last post by:
Hi, I'm having a problem with event handlers. My code looks (a bit simplified) like this: public class Listener { CallerClass Call = new CallerClass(); string data= ""; public Listener()
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
10
by: fusillo | last post by:
Hi, i've tried a test code about the Event Listener API but i've some problem about detaching the element firing the event with IE. Here's my code: <html> <head> <style type="text/css">...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
3
by: MC | last post by:
Question, Why does the following code register the event for input type button but not radio? (And how do I make this happen?) Thanks, MC <HTML> <FORM name='x' action=''> <input type='radio'...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.