Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 29th, 2003, 07:30 AM
Authorized User
Join Date: Nov 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic HTML <<Events>>


I would like to dynamically add an Event call to an HTML object (say INPUT object). How do I do this?

Consider an example:
<INPUT type="button" onclick="javascript:fnDoAction()" id="btnAdd">
btnAdd.onclick = "javascript:fnDoAction2()" //This does not work!!


Old December 29th, 2003, 08:03 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

The btnAdd button is not directly visible in the page. You'll need to get a reference to it, using document.getElementById or by using formName.buttonName. The following code shows you how to accomplish this. It uses the onload event of the <body> tag to set the dynamic event for the button. When you click the button, method 2 will fire instead of method 1:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <title>Dynamic Events</title>
    <script language="JavaScript" type="text/javascript">
        function fnDoAction()
            alert('I am action 1');

        function DoAction2()
            alert('I am action 2');

        function SetDynamicEvent()
            document.getElementById('btnAdd').onclick = DoAction2;
            // Alternatively, uncomment the next code line. Old code style
            // but cross-browser.
            // frmDynamicEvents.btnAdd.onclick = DoAction2;
<body onload="JavaScript:SetDynamicEvent();">
    <form name="frmDynamicEvents">
        <input type="button" onclick="JavasSript:fnDoAction()" id="btnAdd" value="Click Me">


Imar Spaanjaars
Everyone is unique, except for me.
Old December 29th, 2003, 12:40 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik

u can use

Hovik Melkomian.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 8: <asp:image> inside <a> & ext.CSS (pg. 274) epc BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2008 04:37 AM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
<marquee><b>About CHAT App. in PHP4</b></marquee> Ramkrishna PHP How-To 1 September 11th, 2004 07:01 AM
<STRONG> vs <B> and <EM> vs <I> anshul HTML Code Clinic 12 September 1st, 2004 05:22 PM
a problem in book<<beginning asp.net using vb>> luoware ASP.NET 1.0 and 1.1 Basics 3 December 8th, 2003 09:32 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.