To following code/script shows me the row number of the checkbox I've just
clicked:
<form name = "my_form" action = "" method = "post" accept-charset =
"iso-8859-1">
<table name = "my_table" border = "1">
<tr>
<td>
<input type = "checkbox" onClick = "alert('Row 1');">
</td>
</tr>
<tr>
<td>
<input type = "checkbox" onClick = "alert('Row 2');">
</td>
</tr>
</table>
</form>
That works fine but I'd like to do it dynamically.
I tried
alert(document. my_form.my_tabl e.row);
but it doesn't work (Error: document.my_for m.my_table has no properties). I
think the problem is that <table> does not have a 'name' attribute.
I need this because I wrote some Java Scripts to sort a html table.
The data are visible on the screen (html table) and are also stored in an
array. If I click now on a checkbox I need to now which row of my array
'array[row]' I have to modify (modify the flag of the checkbox). That's the
reason I need to know on which row I've clicked the checkbox.
Does anyone know how I can get the row dynamically?
Stefan 4 1683
Stefan Mueller wrote: Does anyone know how I can get the row dynamically?
The trick lies in the event object. Accessing it differs slightly
between IE and Firefox. In IE, you can access it with window.event. In
Mozilla/Firefox, the event gets passed as an argument to the event
handler.
Assuming the IE example, because it's a little simpler:
Once you have the event, you can get the source element:
clickedCheckbox = window.event.sr cEvent;
From there, you can walk up the node tree using the parentNode
property.
In your example:
tableCell = clickedCheckbox .parentNode; // this is the td
tableRow = tableCell.paren tNode; // this is the row
Or, to simplify the code into one line:
clickedRow = window.event.sr cEvent.parentNo de.parentNode;
Stefan Mueller wrote: To following code/script shows me the row number of the checkbox I've just clicked:
<form name = "my_form" action = "" method = "post" accept-charset = "iso-8859-1"> <table name = "my_table" border = "1"> <tr> <td> <input type = "checkbox" onClick = "alert('Row 1');"> </td> </tr> <tr> <td> <input type = "checkbox" onClick = "alert('Row 2');"> </td> </tr> </table> </form>
That works fine but I'd like to do it dynamically.
I tried alert(document. my_form.my_tabl e.row); but it doesn't work (Error: document.my_for m.my_table has no properties). I think the problem is that <table> does not have a 'name' attribute.
I need this because I wrote some Java Scripts to sort a html table. The data are visible on the screen (html table) and are also stored in an array. If I click now on a checkbox I need to now which row of my array 'array[row]' I have to modify (modify the flag of the checkbox). That's the reason I need to know on which row I've clicked the checkbox.
Does anyone know how I can get the row dynamically?
Go up the DOM tree until you hit the first TR element.
function showRowIndex(el )
{
while (el.parentNode && 'tr' != el.nodeName.toL owerCase()){
el = el.parentNode;
}
// If found a TR parent, return its index
if ('tr' == el.nodeName.toL owerCase()) return el.rowIndex;
// Otherwise, el doesn't have a TR parent.
return "No TR parent";
}
Then on your checkbox:
<input type = "checkbox" onclick="alert( showRowIndex(th is));">
--
Rob
Joe Attardi wrote: Stefan Mueller wrote:
Does anyone know how I can get the row dynamically?
The trick lies in the event object. Accessing it differs slightly between IE and Firefox. In IE, you can access it with window.event. In Mozilla/Firefox, the event gets passed as an argument to the event handler.
Much simpler to use 'this' when calling the function to get a reference
directly to the element that fired the event. Assuming the IE example, because it's a little simpler:
Not really, it's just different to the W3C model. If event is to be
used it's usually done like this:
<input type="checkbox" onclick="someFn (event);" ... >
function someFn(e)
{
var e = e || window.event;
var t = e.target || e.srcElement;
// t is the element that was clicked on
}
Now you have a reference to the element that was clicked on in browsers
that use either the W3C or IE event model. However, the element that
was clicked on may not be the element that fired the event, it may be a
decedent.
If you are really after the element that fired the event, that is not a
good way to get it.
Once you have the event, you can get the source element: clickedCheckbox = window.event.sr cEvent;
Simply pass 'this' from the checkbox in the first place:
<input type="checkbox" onclick="someFn (this);" ... >
function someFn(el)
{
// el is a reference to the checkbox that fired the event
}
No messing around with event models.
[...]
--
Rob
Many thanks for your help and your hints.
Today I do it with
while ('TR' != this.nodeName.t oUpperCase() ) {this = this.parentNode ;}
alert (this.rowIndex) ;
Stefan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: loga123 |
last post by:
Hi All,
I am very new to .net and trying to develop a small application.
I am using asp .net 2.0.
I have a table control (server) in my code that has header row like
this.
<asp:Table id="Table1"
GridLines="Both"
|
by: Ian Davies |
last post by:
Hello
Needing help with a suitable solution.
I have extracted records into a table under three columns 'category',
'comment' and share (the category column also holds the index no of the
record in a hidden field)
I wish the user to be able to edit the data in the table, so I have
extracted the records into hiddenfield, textareas, dropdown list and
checkbox so that they can make changes.
I named these elements as arrays and wish to run an...
|
by: dave |
last post by:
I have a html table and mutliple rows. On each row i put an onclick event
that opesn a modal window and prompts the user for some information.
I want this modal window to show when the user clicks anything on that row.
I have a few checkboxes in the row cells.
The click event first fires on the checkbox and then the row (opening the
modal window after the checkbox is checked).
How can I force the modal window to open first and...
|
by: Man4ish |
last post by:
I am trying to create a table with three columns.
first two columns will be having text values while last column has checkbox. How it can implemented? Any example doing the same will really help me.
Thanks in advance.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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: 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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |