473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.createEleme nt("TD");
newbutton = doc.createEleme nt("input");
newbutton.name = "newChange"+__u id;
newbutton.width = 15;
newbutton.heigh t = 15;
newbutton.type = "button";
newbutton.oncli ck = "alert('hi' );";
newcol.appendCh ild(newbutton);
alert(newcol.in nerHTML);
newrow.appendCh ild(newcol);
tbl.appendChild (newrow);
__uid++;
window.close();

Any help will be greatly appreciated.

Thanks
Dipin
Jul 20 '05 #1
4 39457
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.createEleme nt("TD");
newbutton = doc.createEleme nt("input");
newbutton.name = "newChange"+__u id;
newbutton.width = 15;
newbutton.heigh t = 15;
newbutton.type = "button";
newbutton.oncli ck = "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.oncli ck=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.createEleme nt("TD");
newbutton = doc.createEleme nt("input");
newbutton.name = "newChange"+__u id;
newbutton.width = 15;
newbutton.heigh t = 15;
newbutton.type = "button";
newbutton.oncli ck = "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.oncli ck=new Function("alert ('hi')");


The much better way is

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

http://www.crockford.com/javascript/inheritance.html
Jul 20 '05 #3
dc*******@profe ssionalaccess.c om (Dipin) wrote in message news:<be******* *************** ****@posting.go ogle.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.createEleme nt("TD");
newbutton = doc.createEleme nt("input");
newbutton.name = "newChange"+__u id;
newbutton.width = 15;
newbutton.heigh t = 15;
newbutton.type = "button";
newbutton.oncli ck = "alert('hi' );";
newcol.appendCh ild(newbutton);
alert(newcol.in nerHTML);
newrow.appendCh ild(newcol);
tbl.appendChild (newrow);
__uid++;
window.close();

Any help will be greatly appreciated.

Thanks
Dipin


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

<input type="button" id='btn1' value='string' /><br>
<input type="button"id ='btn2' value='anonymou s script block'
onclick='return clickHandler(); '/><br>
<input type="button" id='btn3' value='showFunc tion(btn1)'
onclick="showFu nction(document .getElementById ('btn1'));" /><br>
<input type="button" id='btn4' value='showFunc tion(btn2)'
onclick="showFu nction(document .getElementById ('btn2'));" />
<script type='text/javascript'>
function clickHandler(){
alert('in clickhandler');
return 1;
}
function showFunction(bt n){
alert(btn.oncli ck.toString() + '\ntype: ' + typeof(btn.oncl ick));
}
document.getEle mentById('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.createEleme nt("TD");
newcol = newrow.insertCe ll(index);
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016
newbutton = doc.createEleme nt("input");
newbutton.name = "newChange"+__u id;
newbutton.width = 15;
newbutton.heigh t = 15;
newbutton.type = "button";
newbutton.oncli ck = "alert('hi' );";
newbutton.oncli ck = new Function(evt)
{alert("hi");};
newcol.appendCh ild(newbutton);
alert(newcol.in nerHTML);
Not necessary.
newrow.appendCh ild(newcol);
with insertCell(inde x), 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
3165
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 row has been added is itself contained within an outer table (to handle the desired screen layout). That outer table does not properly grow to contain the new size of the table to which the row was added. (More specifically, I have...
1
4857
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 onclik=Alert("EditFieldValue2.value"); Please reply me as soon as possible.
1
17684
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
5
19667
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. Any suggestions? I tried with btnAdd.Attributes.Add("onClick","AddItem();")
1
1550
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 type='text'> object 02) by using appendChild () method, I've successfully append the button and the textbox under the div tag (or container in this case) 03) but now I would like to add the javascript onclick event on the button I've just created...
7
2797
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 following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
1
2604
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 retrive value from array. I am very poore in javascript please help me. coding is like this
1
6058
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 to a DIV within the form. Here is an example I store from another post. I hope this helps someone. <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
3
5401
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 the html page. But if I try to navigate to another pdf in the IFRAME then no caching occurs. Is the problem in the IFRAME reloading instead of just refreshing the pdf? <SCRIPT type="text/javascript"> var pageNo; var nav; var iframe; var...
0
9703
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
9565
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
10317
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
10295
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
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5501
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...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
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...

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.