473,790 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

event handler defined in innerHTML not work

I have defined onclick event handler within the innerHTML property as
following:

var testButton1=oWi n.document.crea teElement("LABE L");
testButton1.inn erHTML="<input type='button' value='xx'
onclick=\"alert ('123')\">";
oLabel.appendCh ild(testButton1 );

the oLabel has already been append to document.body

however, this works in some computer but some are not. I've tried
computer with the same IE patch (6.0.2800.1106, sp1;Q867801;Q82 3353)
and the same windows version (2000 sp4),but this code just have
different behavior.

Is there anything I miss? Thank you!
Jul 23 '05 #1
4 2063
lendle wrote:
I have defined onclick event handler within the innerHTML property as
following:

var testButton1=oWi n.document.crea teElement("LABE L");
testButton1.inn erHTML="<input type='button' value='xx'
onclick=\"alert ('123')\">";
oLabel.appendCh ild(testButton1 );

the oLabel has already been append to document.body


You are creating invalid HTML. You are basically generating:

<label><input type='button' value='xx' onclick="alert( '123')"></label>

Which makes no sense at all. A <label> can't contain an <input>. You are
then appending that mess as the child of another label (oLabel), so you
are ending up with some HTML that probably looks something like:

<label><label>< input type='button' value='xx'
onclick="alert( '123')"></label></label>

It appears you want to create a button and put it on document.body. So do
that. Create the button and append it to document.body, NOT to a <label>:

<script type="text/javascript">
var testButton1 = document.create Element('input' );
testButton1.typ e = 'button';
testButton1.val ue = 'xx';
testButton1.onc lick = function() { alert('123'); };
document.body.a ppendChild(test Button1);
</script>

Tested and working in IE 6.0.2800 (pre-XPSP2) and 6.0.2900 (XPSP2),
Firefox 0.9.3 and Opera 7.54.

I'm guessing you took some code you did not understand that created a
<label>, set it's innerHTML and appended the <label> to the previous
element. Now you assume that everytime you create an element, it has to
be a <label>, you have to set it's innerHTML, and you have to append it
to the previous element.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #2
On Tue, 14 Sep 2004 15:43:41 GMT, Grant Wagner
<gw*****@agrico reunited.com> wrote:

[snip]
You are creating invalid HTML. You are basically generating:

<label><input type='button' value='xx' onclick="alert( '123')">
</label>

Which makes no sense at all. A <label> can't contain an <input>.
Yes it can. The LABEL element can contain any inline element, except other
LABELs. The HTML Specification even includes an example with LABEL acting
as a container.

If I remember correctly, it's preferable to use both the for attribute,
and LABEL as a container, to annotate a form control. This allows browsers
that only understand one or the other to function correctly.
You are then appending that mess as the child of another label (oLabel),
so you are ending up with some HTML that probably looks something like:

<label><label>< input type='button' value='xx'
onclick="alert( '123')"></label></label>
If your interpretation of the OP's post is correct, then that certainly is
invalid HTML.
It appears you want to create a button and put it on document.body. So
do that. Create the button and append it to document.body, NOT to a
<label>:


As I said, there's nothing wrong with using a LABEL as a container. It is,
in fact, recommended. Be sure to use for, too.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter wrote:
On Tue, 14 Sep 2004 15:43:41 GMT, Grant Wagner
<gw*****@agrico reunited.com> wrote:
You are creating invalid HTML. You are basically generating:

<label><input type='button' value='xx' onclick="alert( '123')">
</label>

Which makes no sense at all. A <label> can't contain an <input>.


Yes it can. The LABEL element can contain any inline element, except other
LABELs. The HTML Specification even includes an example with LABEL acting
as a container.


I didn't have Internet access while I was composing my reply and for some
reason, managed to miss "INPUT" as a "Can Contain" for "LABEL" in my HTML 4.0
Sourcebook. You are of course right.
It appears you want to create a button and put it on document.body. So
do that. Create the button and append it to document.body, NOT to a
<label>:


As I said, there's nothing wrong with using a LABEL as a container. It is,
in fact, recommended. Be sure to use for, too.


And I'd still recommend sticking to document.create Element() and appendChild()
rather than mixing it with innerHTML, especially in a simple example like the
one shown (appending an input to a label, appending that label to some other
element).

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #4
Thanks, Grant Wagner,
you are right, I insert my code into the code written by my colleague, and I
insert into wrong place :p
Your code worked, however, if I change my code to

<script language="JavaS cript">
var obj=window.docu ment.createElem ent("form");
obj.innerHTML=" <input type='button' value='xx' onclick=\"alert ('123')\">";
window.document .body.appendChi ld(obj);
</script>

it still not work on IE, but work on Mozilla and Opera.
I wonder if there is any problem about system settings I should check?
Thank you!
Jul 23 '05 #5

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

Similar topics

7
49583
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
10
3607
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
4
4917
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello'); window.resizeTo(810,750); top.outerWidth=810;
6
1877
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
41
4331
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
3
3987
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having a couple of problems, which I'll try to describe. But this e-mail will only reproduce one of them, in a "short" example. What I'm generally doing is having each form entry contained in a div, which as a label, an input with some event handlers,...
8
2819
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
7
2533
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written several methods that are working fine. The problem is if I want to dynamically assign an event handler to the object, the event handler is not called. What am I doing wrong? Below is a sample HTML doc with everything in place. <!DOCTYPE HTML PUBLIC...
6
1828
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool. One of the requirements is that the calendar will need to display a varying number of months (1..3)
0
9666
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
9512
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
10200
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
10145
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
9021
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...
1
7530
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
6769
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();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.