Hi Guys,
I'm trying to set an eventlistener for a bunch of DIV tags. can you tell me why this code wont work? -
display=document.getElementById("display");
-
field=display.getElementsByTagName('div');
-
-
for (var i in field){
-
if (field[i].addEventListener)
-
{
-
field[i].addEventListener('click',go(),false);
-
}
-
else if (field[i].attachEvent)
-
{
-
field[i].attachEvent('onclick',go());
-
}
right now as soon as the page loads it triggers go() (which is a page redirect function)
7 4723
You are telling it to do so-
replace go() with go
The parens call the function immediately
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...
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) - function bindMethod(func,args){
-
var tmp=args
-
return func(tmp)
-
}
-
-
addEventListener('click',bindMethod(go,myArguments),false)
-
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....
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) - function bindMethod(func,args){
-
var tmp=args
-
return func(tmp)
-
}
-
-
addEventListener('click',bindMethod(go,myArguments),false)
-
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.
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?
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.
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: - var myUrl='http://someplace.else'
-
myDiv.onmouseup=function(){ go(myUrl) }
-
-
go=function(url,hash){
-
window.location.href=url
-
window.location.hash=hash || ''
-
}
for the current window or just: - go=function(url){
-
window.open(url)
-
}
the following link has some really good info on the location property: DevGuru : Location Sign in to post your reply or Sign up for a free account.
Similar topics
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:
...
|
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?...
|
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...
|
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...
|
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()
|
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...
|
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">...
|
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...
|
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'...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
| |