For an abstract algebra class that I will be teaching,
I'm trying to convert a text-based game that I wrote
in Common Lisp to a graphical game in javascript. (I
know nothing of javascript, but I have three books
from the library that I have been s*l*o*w*l*y going
through.)
Currently I have a large, say, 30x20, table
<table id="GameTable" >
<tr<td>A</td<td>B</td<td>C</td... </tr>
<tr<td>D</td<td>E</td<td>F</td... </tr>
...
</table>
I'm accessing elements via
var game_table = document.getEle mentById('GameT able');
var curr_cell;
function game_node(x,y) {
curr_cell = game_table.rows[x].cells[y];
return(curr_cel l.firstChild);
};
and I'm able to write/read to
game_node(x,y). nodeValue
just fine. But here's where I'm stuck:
I'd like, when the user clicks on, say, cell (3,5),
that a function-call
UpdateGame(3,5)
is invoked. Is there a way I can do this NEITHER
having an "onclick=" NOR an "id=" for each and every
<td>? I seek something like having a single "id=" and
"onclick=" for the entire table, e.g,
<table id="GameTable"
onclick="Update Game(this.rowIn dex, this.colIndex)" >
where -somehow- variables rowIndex and colIndex were
automagically set by the mouse-click.
Ta, Jonathan King Math dept, Univ. of Florida 8 2268
You can use event.target/event.srcElemen t to determine the clicked
node and check if it is inside the table. Then you can use cellIndex
and rowIndex properties of the cell and row to get the coordinates.
Simple sample (function to handle the click must be set after the
element id="GameTable" is loaded):
document.getEle mentById("GameT able").onclick = function(e) {
e = e || window.event;
for (var td = e.target || e.srcElement; td && td != this; td =
td.parentNode)
if (td.tagName == "TD" && td.parentNode.p arentNode.paren tNode ==
this) {
UpdateGame(td.p arentNode.rowIn dex, td.cellIndex);
break;
}
}
On May 18, 11:37 pm, "P. Prikryl" <p.prik...@gmai l.comwrote:
You can use event.target/event.srcElemen t to determine the clicked
node and check if it is inside the table. Then you can use cellIndex
and rowIndex properties of the cell and row to get the coordinates.
For Google food, this has been termed "event delegation" meaning a
parent element is watching for events that bubble up from child
elements. http://yuiblog.com/blog/2007/01/17/event-plan/ http://developer.yahoo.com/yui/examp...elegation.html
Peter
Peter Michaux wrote:
On May 18, 11:37 pm, "P. Prikryl" <p.prik...@gmai l.comwrote:
>You can use event.target/event.srcElemen t to determine the clicked node and check if it is inside the table. Then you can use cellIndex and rowIndex properties of the cell and row to get the coordinates.
For Google food, this has been termed "event delegation" meaning a
parent element is watching for events that bubble up from child
elements.
http://yuiblog.com/blog/2007/01/17/event-plan/ http://developer.yahoo.com/yui/examp...elegation.html
However, the technically correct no-buzzword term is "Event bubbling"
(mentioned here before) or, more generally, "Event dispatch": http://www.w3.org/TR/DOM-Level-2-Eve...ml#Events-flow http://www.w3.org/TR/2007/WD-DOM-Lev...vents-20071221
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
On May 19, 2:37 am, "P. Prikryl" <p.prik...@gmai l.comwrote:
You can use event.target/event.srcElemen t to determine ...
Thank you (dziekuje bardzo) P. Prikryl, Peter and Thomas.
The code works for me in FF under MacOSX, and there are a
few places where I don't know how/why it works. Currently,
inside <BODY></BODYI have
<table id="GameTable" >
<tr<td>AA</td<td>BB</td<td>CC</td</tr>
<tr<td>DD</td<td>EE</td<td>FF</td</tr>
</table>
<script language="JavaS cript">
function UpdateGame(x,y) {alert("USER clicked (" + x + "," + y +
").")};
document.getEle mentById("GameT able").onclick = function(e) {
e = e || window.event;
for (var tdvar = e.target || e.srcElement;
tdvar && tdvar != this;
tdvar = tdvar.parentNod e)
if (tdvar.tagName == "TD" &&
tdvar.parentNod e.parentNode.pa rentNode == this) {
UpdateGame(tdva r.parentNode.ro wIndex, tdvar.cellIndex );
break;
}
};
</script>
Q1: Why is "TD" capitalized? I understand that HTML is
case-insensitive whereas JS is sensitive. The ==
comparison is done at the JS level; how did you know to use
uppercase?
Q2: Why 3 levels of parentNode, rather than 2, in
tdvar.parentNod e.parentNode.pa rentNode == this
? Is it that `tdvar' is bound to an event whose parent is
<TD>? Or is it that `tdvar' is bound to <TD>, but that an
implicit <TBODYis being inserted into my innocent
<table>? --I am inferring the latter from this page: http://htmlhelp.com/reference/html40/tables/tbody.html
I haven't looked at HTML for a l*o*n*g time, I don't
remember there even being a <TBODYtag when I was
designing the few tables I use on my site.
Ta, Jonathan King Math dept, Univ. of Florida
sq*******@math. ufl.edu escribió:
Q1: Why is "TD" capitalized? I understand that HTML is
case-insensitive whereas JS is sensitive. The ==
comparison is done at the JS level; how did you know to use
uppercase?
You've said it all, that's how JavaScript works:
>>"a"=="A"
false
>>"A"=="A"
true
Q2: Why 3 levels of parentNode, rather than 2, in
tdvar.parentNod e.parentNode.pa rentNode == this
Again, you got the key yourself: TBODY.
In general, you have an HTML source code that is parsed by the browser
and converted into a DOM tree. What you can manipulate with JavaScript
is the resulting node tree. This becomes more evident if you have
invalid markup like "<div>Foo</p>".
I suggest you install the Firebug extension for Firefox and have a look
at the DOM tree. Once you get used to Firebug you can't code without it.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
On May 19, 6:23 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
Peter Michaux wrote:
On May 18, 11:37 pm, "P. Prikryl" <p.prik...@gmai l.comwrote:
You can use event.target/event.srcElemen t to determine the clicked
node and check if it is inside the table. Then you can use cellIndex
and rowIndex properties of the cell and row to get the coordinates.
For Google food, this has been termed "event delegation" meaning a
parent element is watching for events that bubble up from child
elements.
http://yuiblog.com/blog/2007/01/17/event-plan/ http://developer.yahoo.com/yui/examp...elegation.html
However, the technically correct no-buzzword term is "Event bubbling"
(mentioned here before) or, more generally, "Event dispatch":
http://www.w3.org/TR/DOM-Level-2-Eve...vents-20071221
"Event bubbling" is the mechanics of the chain of responsibility
pattern implemented in the DOM. It is just the fact that an event
does bubble.
"Event delegation" is an application-level concept of how a bubbling
event is used. If a ancestor node is handling an event on behalf of a
node that would normally handle the event itself then that is event
delegation. Event bubbling is what makes event delegation possible.
Peter
Peter Michaux wrote:
On May 19, 6:23 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
>Peter Michaux wrote:
>>On May 18, 11:37 pm, "P. Prikryl" <p.prik...@gmai l.comwrote: You can use event.target/event.srcElemen t to determine the clicked node and check if it is inside the table. Then you can use cellIndex and rowIndex properties of the cell and row to get the coordinates. For Google food, this has been termed "event delegation" meaning a parent element is watching for events that bubble up from child elements. http://yuiblog.com/blog/2007/01/17/event-plan/ http://developer.yahoo.com/yui/examp...elegation.html
However, the technically correct no-buzzword term is "Event bubbling" (mentioned here before) or, more generally, "Event dispatch":
http://www.w3.org/TR/DOM-Level-2-Eve...vents-20071221
"Event bubbling" is the mechanics of the chain of responsibility
pattern implemented in the DOM. It is just the fact that an event
does bubble.
"Event delegation" is an application-level concept of how a bubbling
event is used. If a ancestor node is handling an event on behalf of a
node that would normally handle the event itself then that is event
delegation. Event bubbling is what makes event delegation possible.
Read again.
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
On May 19, 12:35 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
....
Must be at least
<script type="text/javascript">
The `language' attribute is deprecated, the `type' attribute is required:
Ah; ta,
Q1: Why is "TD" capitalized? ...
See <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-822762427>.
However, as XHTML may also be served as text/html in which case it may
be parsed as HTML, I consider case-insensitive matching to be less
error-prone:
if (tdvar.tagName. toLowerCase() == "td" ...)
Good; I'm going to make this my std practice.
I haven't yet processed your more technical comments; I'm still
reading
the textbooks. Thanks again.
Jonathan King Math dept, Univ. of Florida This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Raptor |
last post by:
I'm using a single script to generate a table with <input>s in each row.
I fill the array with initial values, then write it out to the table
and let the user edit the values. Something like:
$myarray = $array(1, 2, 3, ... 100);
echo 'Enter your changes, then click Submit:';
foreach ($array as $i)
echo '<table tags> <input value="'.$i.'" name="index.'$i.'"> <table
tags>';
|
by: Adam Hartshorne |
last post by:
As a result of a graphics based algorihtms, I have a list of indices to
a set of nodes.
I want to efficiently identify any node indices that are stored multiple
times in the array and the location of them in the array /list. Hence
the output being some list of lists, containing groups of indices of the
storage array that point to the same node index.
This is obviously a trivial problem, but if my storage list is large and
the set of...
|
by: beachnut |
last post by:
Hi, all.
This should be pretty easy:
When parsing my XmlDocument object with a validating
reader, what's the proper way to detect when it
"exits" an element's block? It's the first time
I've needed to toggle a bool when entering and
exiting one; I tried to trap the exit with
'case "</block>":', but that doesn't work.
|
by: AndrewD |
last post by:
Hi,
Using scanf, is it possible to detect input numbers where the decimal
count is too large?
eg.
scanf("%19lg",&var);
|
by: Sam-Kiwi |
last post by:
I've spent the last 6 months developing a pay-per-download website using
ASP.NET
Users purchase documents and then download them.
The intention is that users are only charged for documents they successfuly
download.
My problem revolves around detecting a successful download, the steps I take
to handle the download are as follows:
| |
by: jab3 |
last post by:
Hello. I'm wondering if someone can answer something I'm sure has been
answered a thousand times before. I am apparently just too dumb to
find the answer. :) I've found information about the 'onstop' event,
but it's not behaving as expected. (And it also seems to be a
proprietary attribute) That is, my defined function is not being run
when I click stop. I've 'inserted' it like this:
<body onstop="stopped_clicked()">
And the function...
|
by: petermichaux |
last post by:
Hi,
I don't know if there is a way to feature detect for this or not. I
have a list and the user can click on items to select. I would like
that they can command-click on Mac or control-click on other OS to
multiple select. This matches normal file system behavior.
I found this example which doesn't seem to work in Opera on Mac: the
mac variable will be false.
|
by: Ming |
last post by:
For example, 100,000 records, each record has 10 fields and may belong
to one or more categories. How shall I save those records in DB? I
think it is a very typical concern for most online shopping mall.
Option 1)
A table with all 100,000 records, and 10 fields for each record
Option 2)
|
by: RobG |
last post by:
When using createEvent, an eventType parameter must be provided as an
argument. This can be one of those specified in DOM 2 or 3 Events, or
it might be a proprietary eventType.
My problem is testing for support of particular eventTypes - the DOM 2
Events Interface DocumentEvent says that if the eventType is not
supported, it throws a DOM exception. This makes testing rather
tough - if you try something like:
if (document &&...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |