473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

dynamic button onclick event not working

Hi All;

I have this javascript which is adding a new button to the column in
the row which is created dynamically, the innerhtml shows that the
onclick event is correctly added but it never gets invoked when I
click it.

newcol = doc.createElement("TD");
newbutton = doc.createElement("input");
newbutton.name = "newChange"+__uid;
newbutton.width = 15;
newbutton.height = 15;
newbutton.type = "button";
newbutton.onclick = "alert('hi');";
newcol.appendChild(newbutton);
alert(newcol.innerHTML);
newrow.appendChild(newcol);
tbl.appendChild(newrow);
__uid++;
window.close();

Any help will be greatly appreciated.

Thanks
Dipin
Jul 20 '05 #1
4 39430
Lee
Dipin said:

Hi All;

I have this javascript which is adding a new button to the column in
the row which is created dynamically, the innerhtml shows that the
onclick event is correctly added but it never gets invoked when I
click it.

newcol = doc.createElement("TD");
newbutton = doc.createElement("input");
newbutton.name = "newChange"+__uid;
newbutton.width = 15;
newbutton.height = 15;
newbutton.type = "button";
newbutton.onclick = "alert('hi');";


In HTML, the onclick attribute has a string value, but in
JavaScript, it is not a string, but a reference to a Function.
One way to accomplish this is:

newbutton.onclick=new Function("alert('hi')");

Jul 20 '05 #2
> >I have this javascript which is adding a new button to the column in
the row which is created dynamically, the innerhtml shows that the
onclick event is correctly added but it never gets invoked when I
click it.

newcol = doc.createElement("TD");
newbutton = doc.createElement("input");
newbutton.name = "newChange"+__uid;
newbutton.width = 15;
newbutton.height = 15;
newbutton.type = "button";
newbutton.onclick = "alert('hi');";


In HTML, the onclick attribute has a string value, but in
JavaScript, it is not a string, but a reference to a Function.
One way to accomplish this is:

newbutton.onclick=new Function("alert('hi')");


The much better way is

newbutton.onclick = function () {
alert('hi');
};

http://www.crockford.com/javascript/inheritance.html
Jul 20 '05 #3
dc*******@professionalaccess.com (Dipin) wrote in message news:<be**************************@posting.google. com>...
Hi All;

I have this javascript which is adding a new button to the column in
the row which is created dynamically, the innerhtml shows that the
onclick event is correctly added but it never gets invoked when I
click it.

newcol = doc.createElement("TD");
newbutton = doc.createElement("input");
newbutton.name = "newChange"+__uid;
newbutton.width = 15;
newbutton.height = 15;
newbutton.type = "button";
newbutton.onclick = "alert('hi');";
newcol.appendChild(newbutton);
alert(newcol.innerHTML);
newrow.appendChild(newcol);
tbl.appendChild(newrow);
__uid++;
window.close();

Any help will be greatly appreciated.

Thanks
Dipin


Aren't you assigning a string to newbutton.onclick? I presume that
typeof(newbutton.onclick) is string:

<input type="button" id='btn1' value='string' /><br>
<input type="button"id='btn2' value='anonymous script block'
onclick='return clickHandler();'/><br>
<input type="button" id='btn3' value='showFunction(btn1)'
onclick="showFunction(document.getElementById('btn 1'));" /><br>
<input type="button" id='btn4' value='showFunction(btn2)'
onclick="showFunction(document.getElementById('btn 2'));" />
<script type='text/javascript'>
function clickHandler(){
alert('in clickhandler');
return 1;
}
function showFunction(btn){
alert(btn.onclick.toString() + '\ntype: ' + typeof(btn.onclick));
}
document.getElementById('btn1').onclick = 'return clickHandler();';
</script>
Jul 20 '05 #4
DU
Dipin wrote:
Hi All;

I have this javascript which is adding a new button to the column in
the row which is created dynamically, the innerhtml shows that the
onclick event is correctly added but it never gets invoked when I
click it.

newcol = doc.createElement("TD");
newcol = newrow.insertCell(index);
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016
newbutton = doc.createElement("input");
newbutton.name = "newChange"+__uid;
newbutton.width = 15;
newbutton.height = 15;
newbutton.type = "button";
newbutton.onclick = "alert('hi');";
newbutton.onclick = new Function(evt)
{alert("hi");};
newcol.appendChild(newbutton);
alert(newcol.innerHTML);
Not necessary.
newrow.appendChild(newcol);
with insertCell(index), the above instruction is no longer needed.
tbl.appendChild(newrow);
Also, much performant is insertRow(index);
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903
insertCell() and insertRow are very well supported among recent browser
versions.
__uid++;
window.close();
You're closing the window? Why?

Any help will be greatly appreciated.

Thanks
Dipin

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #5

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

Similar topics

3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
1
by: SAN CAZIANO | last post by:
I have create a dynamic html table by adding some rows where I have to put some value in an input field and now how can I get all value of all row ??? I try this but it doesn' work button...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
5
by: RA | last post by:
I have created a button dynamically; which has been added to a TableCell of a TableRow of a Table control. Is there a way to add onclick event which calls a procedure on the Server-side itself....
1
by: kusanagihk | last post by:
To all, I'm working on a javascript to dynamic build a common set of HTML controls. 01) I've used the DOM object to build a <div> tag; then build 1 <input type='button'/> and 1 <input...
7
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The...
1
by: mandakini | last post by:
Hello freinds I am working on this url http://72.36.156.243/compbuild.php Here I am using ifram and displaying dynamic value I don't know how to use iframe as array how to assign array and...
1
by: Derek Basch | last post by:
I spent several hours struggling with dynamic form fields added with appendChild or innerHTML not POSTing on submit in Firefox. The only way I found to make it work is to append any created fields...
3
polymorphic
by: polymorphic | last post by:
I have succeeded in embedding PDF files in a dynamic iframe. The problem is that I need the PDF to cache. If the PDF remains the same from page load to page load then the pdf is somehow cached with...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...

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.