User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 374,013 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,687 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 33122 | Replies: 10
Reply
Join Date: May 2005
Posts: 3
Reputation: zaki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
zaki zaki is offline Offline
Newbie Poster

Help add onclick event programmatically

  #1  
May 10th, 2005
I have a code that I call from Button_click event.
The code adds a new row to the table and attempts
to add onclick element to every td element in the new row.
Somehow, onclick does not work.
Any help is appreciated.
Here is the code:
function insRow()
{
var x = document.getElementById('Table1').insertRow(1);
for(i=0;i<4;i++)
{
var td = document.createElement('td');
td.innerHTML = 'NewItem';
td.setAttribute('onclick', 'alert("blabla")');
x.appendChild(td);
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: add onclick event programmatically

  #2  
May 12th, 2005
hey,
your code works in firefox. so i'd say it pretty close to working in IE. the only change i had to make was in regards to the table row index.. but i presume you already have a row in your table.
Reply With Quote  
Join Date: May 2005
Posts: 3
Reputation: zaki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
zaki zaki is offline Offline
Newbie Poster

Solution Re: add onclick event programmatically

  #3  
May 12th, 2005
Originally Posted by alpha_foobar
hey,
your code works in firefox. so i'd say it pretty close to working in IE. the only change i had to make was in regards to the table row index.. but i presume you already have a row in your table.

The first line of my code adds a new row to the table.
I don't know what is wrong with the code, it just doesn't work...
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: add onclick event programmatically

  #4  
May 12th, 2005
i changed insertRow(1); to insertRow(0); and it added the row in firefox. (1 is the second index of a table row).

when i added it, it worked in firefox on linux... so your not far off.
Reply With Quote  
Join Date: May 2005
Posts: 3
Reputation: zaki is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
zaki zaki is offline Offline
Newbie Poster

Re: add onclick event programmatically

  #5  
May 13th, 2005
Did you check if the cells in the new row have onclick event associated with them, that what my problem is, not that the row didn't appear. Thanks for your help, anyway.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: add onclick event programmatically

  #6  
May 14th, 2005
yeah, thats what i'm saying.. it worked in mozilla.. you can get the windows jscript debugger.. i find it a hunk of $h!t so i use firefox and the venkman debugger...

but if you place the javascript keyword debugger in your code, and use the ms debugger it works ok.

and everything works in moz.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: add onclick event programmatically

  #7  
May 26th, 2005
the function attachEvent is IE specific, so if this code still doesn't work, this is a possible solution for IE browsers. And could be used as follows:

if( td.attachEvent ){
   td.attachEvent('onclick', 'alert("blabla")');
} else {
   td.setAttribute('onclick', 'alert("blabla")'); 
}
Reply With Quote  
Join Date: May 2005
Posts: 2
Reputation: jchase is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jchase jchase is offline Offline
Newbie Poster

Re: add onclick event programmatically

  #8  
May 26th, 2005
actually, i think you hand in a handle to the function (not a string) and I don't think you can hand in parameters into the function. Thus, check this out to help understand how you can still pass in parameters to an onclick.

function myOnclick() {
sendingObj = event.srcElement;
alert (sendingObj.myProperty);
}

...
img.myProperty = "blahblah";
img.attachEvent("onclick", myOnclick); //notice, no brackets... no parametes, no quotes
Reply With Quote  
Join Date: Jan 2006
Posts: 1
Reputation: talen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
talen talen is offline Offline
Newbie Poster

Re: add onclick event programmatically

  #9  
Jan 11th, 2006
here is what works for me both on mozilla and ie


object.onchange = function() {nameOfYourFunction(parameter1, parameter2, etc)} ;
Reply With Quote  
Join Date: Feb 2008
Posts: 1
Reputation: Singular is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Singular Singular is offline Offline
Newbie Poster

Re: add onclick event programmatically

  #10  
Feb 29th, 2008
Originally Posted by jchase View Post
actually, i think you hand in a handle to the function (not a string) and I don't think you can hand in parameters into the function. Thus, check this out to help understand how you can still pass in parameters to an onclick.

function myOnclick() {
sendingObj = event.srcElement;
alert (sendingObj.myProperty);
}

...
img.myProperty = "blahblah";
img.attachEvent("onclick", myOnclick); //notice, no brackets... no parametes, no quotes


This is correct but you can always use an annonymous function like :

img.attachEvent("onclick", function () {alert('More then one way to skin a cat')});
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC