473,386 Members | 1,962 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,386 software developers and data experts.

event handler defined in innerHTML not work

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

var testButton1=oWin.document.createElement("LABEL");
testButton1.innerHTML="<input type='button' value='xx'
onclick=\"alert('123')\">";
oLabel.appendChild(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;Q823353)
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 2053
lendle wrote:
I have defined onclick event handler within the innerHTML property as
following:

var testButton1=oWin.document.createElement("LABEL");
testButton1.innerHTML="<input type='button' value='xx'
onclick=\"alert('123')\">";
oLabel.appendChild(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.createElement('input');
testButton1.type = 'button';
testButton1.value = 'xx';
testButton1.onclick = function() { alert('123'); };
document.body.appendChild(testButton1);
</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*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #2
On Tue, 14 Sep 2004 15:43:41 GMT, Grant Wagner
<gw*****@agricoreunited.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*****@agricoreunited.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.createElement() 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*****@agricoreunited.com>
comp.lang.javascript 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="JavaScript">
var obj=window.document.createElement("form");
obj.innerHTML="<input type='button' value='xx' onclick=\"alert('123')\">";
window.document.body.appendChild(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
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. ...
10
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...
4
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');...
6
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
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...
3
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...
8
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);"...
7
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...
6
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....
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.