473,761 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding onclick to TR element dynamically

I had this html:

<tr id="action" title="click to do something" onclick="alert( 'mike');">
<td>a</td>
<td>b</td>
</tr>

and it works when I click on the row, but when I try to add it
dynamically like:

myTR=document.c reateElement("T R");
myTR.id="action ";
myTR.title="cli ck to do something";
myTR.onclick="a lert('mike');";
myTD=document.c reateElement("T D");
myText=document .createTextNode ("a");
myTD.appendChil d(myText);
myTD=document.c reateElement("T D");
myText=document .createTextNode ("b");
myTD.appendChil d(myText);
myTR.appendChil d(myTD);
myTABLE.appendC hild(myTR);

Nothing is happening when I click on the row. I know some of the
attribute are case sensitive. I tried onclick, onClick, OnClick, none of
them work.

Anyone know what's up?

Mike

Jul 20 '05 #1
4 44992


Michael Hill wrote:
I had this html:

<tr id="action" title="click to do something" onclick="alert( 'mike');">
<td>a</td>
<td>b</td>
</tr>
Trying
alert(document. getElementById( 'action').oncli ck)
might give you a clue about what is going on in the JavaScript object
model created from that markup above, see below for an explanation.
and it works when I click on the row, but when I try to add it
dynamically like:

myTR=document.c reateElement("T R");
myTR.id="action ";
myTR.title="cli ck to do something";


With client side JavaScript an event handler is a function object so you
need to assign one,
myTR.onclick = function (evt) {
alert('mike');
};
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
>

With client side JavaScript an event handler is a function object so you
need to assign one,
myTR.onclick = function (evt) {
alert('mike');
};


Martin, thanks good stuff execpt instead of "alert('mike'); " I need a real
function like:

myTR.onclick = function (evt) { action_item('ac tion',i); }

The bad news is that in that function the variable 'i', need to be a number.

I have 22 rows that are being produced and I need the onlick event to be
specific for that row! So if I have:

for ( var i=1; i<23; i++ )
{
<snip>
myTR.onclick = function (evt) { action_item('ac tion',i); }
<snip>
}

The resulting html output for each row would look like:

<tr onclick=action_ item('action',' 1')>
<tr onclick=action_ item('action',' 2')>
<tr onclick=action_ item('action',' 3')>
etc.

The value I am getting for each i is "22".

Mike
Jul 20 '05 #3
>
for ( var i=1; i<23; i++ )
{
<snip>
myTR.onclick = function (evt) { action_item('ac tion',i); }
<snip>
}
Mike


This is what I got that works:

myTR.onclick =
function (evt)
{
var tmp=this.id;
var myi=tmp.substr( 6,(tmp.length-6));
action_item('ac tion',myi,'');
}

Anyone have anything else?

Mike

Jul 20 '05 #4


Michael Hill wrote:

I need a real
function like:

myTR.onclick = function (evt) { action_item('ac tion',i); }

The bad news is that in that function the variable 'i', need to be a number.

I have 22 rows that are being produced and I need the onlick event to be
specific for that row! So if I have:

for ( var i=1; i<23; i++ )
{
<snip>
myTR.onclick = function (evt) { action_item('ac tion',i); }
<snip>
}

The resulting html output for each row would look like:

<tr onclick=action_ item('action',' 1')>
<tr onclick=action_ item('action',' 2')>
<tr onclick=action_ item('action',' 3')>
etc.

The value I am getting for each i is "22".


Why do you need a number if that is an index to the <tr> element? Can't
you simply pass the <tr> element object to the function
myTR.onclick = function (evt) {
action_item('ac tion', this);
}
with
function action_item (actionString, actionElement) {
// do things here with actionElement
}

If you really need to pass the current value of say i when the event
handler is created you would need to use the Function constructor to
construct the source of the function as needed
myTR.onclick =
new Function ("evt",
"action_item('a ction', " + i + ");"
);
but in most cases people are doing that they simply miss on passing the
proper object.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
18581
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
10
4887
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world and can't figure out how to make it work in, for instance, Firefox. Right now the code I am using is as follows: newOpt=document.createElement('OPTION'); newOpt.id='optKeyword'; newOpt.label='Keyword';...
8
2944
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or after the class was modified. I need this to work for both old ('classic') and new style classes, at both 2.3 and 2.4. I of course want to avoid side effects, and to make the solution as light-weight as possible.
4
5590
by: Dmitry Korolyov [MVP] | last post by:
When we use btnSubmit.Attributes = "javascript: this.disabled=true;" to make the button disabled and prevent users from clicking it again while form data still posting, there is no longer postback. I.e. the button does go disabled, but the form does not invoke submit() method. Of course, it does work fine without this property. Clues?
0
3174
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where there are tabs at the top with "submenu" buttons showing below the selected tab. The data that defines the tabs and submenus is stored in an XML file and I'm using nested repeaters to build them dynamically. I've got it working pretty well, except...
7
3319
by: Joseph Scoccimaro | last post by:
Currently I am trying to use JavaScript within greasemonkey to dynamically put a menu at the top of each page. I am running in to trouble when I try to append a node representing a script tag to the header section. It ends up not adding anything to the document. I am currently getting no errors from the javascript panel in firefox. Here is the code I am using: HeadTag = document.getElementsByTagName("head"); var scriptElement =...
11
3033
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. It works perfectly (assigning the correct images and the correct onclick events to the correct <atags):
3
3033
by: ICPooreMan | last post by:
The following is a very simple example of what I want to do take an elements oncontextmenu and changing it dynamically onclick of that same element. The code below will fail unless you change the line document.getElementById('div1').setAttribute('oncontextmenu', someText); to document.getElementById('div1').setAttribute('oncontextmenu', function(){alert('World World');return false;});
2
2197
by: romepatel | last post by:
Hello, I am adding a row dynamically to the table , var table = document.getElementById('example'); var rowCount = table.rows.length; var row = table.insertRow(rowCount); now i want to set attributes to the new row added such as id, class etc
0
9522
marktang
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...
0
9336
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,...
0
10111
Oralloy
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...
0
9948
jinu1996
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...
1
9902
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,...
0
9765
tracyyun
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...
1
7327
isladogs
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...
0
6603
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();...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.