473,748 Members | 7,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with calling a function within a newly created element

DL
Hi,

What I wanted to do is to call a function from a newly created
element. But it stumbled me.

Here's the line that references the newly created element and I used
the alert function for debugging for now. Did I mess up all these
quotes?

// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';

Thanks.
Jun 27 '08 #1
22 1695
DL
On May 17, 6:52*pm, DL <tatata9...@gma il.comwrote:
Hi,

What I wanted to do is to call a function from a newly created
element. * But it stumbled me.

Here's the line that references the newly created element and I used
the alert function for debugging for now. * Did I mess up all these
quotes?

// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';

Thanks.
Ok, so, it looks like I wasn't thinking, the Event handler within the
newly created element hasn't been loaded by a browser, hence, no
action upon such an event. Yes? And if so, how do we solve this
problem? Or another better approach to achieve the same goal?

Thanks.
Jun 27 '08 #2
DL wrote:
What I wanted to do is to call a function from a newly created
element. But it stumbled me.

Here's the line that references the newly created element and I used
the alert function for debugging for now. Did I mess up all these
quotes?
Yes, you did.
// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
^start end^ ^start
\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
end^ ^start[1] ^^^[2]
selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';
^end

There. Because you have not properly delimited all your attribute values
[^1], you will generate

<select name=qType42onc hange="alert(i) ;">

if i == 42. What do you expect of the user agent to do here?

While delimiting all attribute values with " --

<select name="qType42"o nchange="alert( i);">

-- or including the missing whitespace --

<select name=qType42 onchange="alert (i);">

-- or do both of them would be a quick fix, I strongly suggest you stop
cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator
and mutator methods instead. With a few user-defined wrappers they can be
even more efficient to use than what you have now.

http://www.w3.org/TR/DOM-Level-2-Core/

Furthermore, if you generate the identifier of `i' instead of the current
value of `i' [^2], it will be resolved on execution of the event handler
attribute value, which is probably not what you want here.

BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO)
should be escaped as `<\/' if the source code is within an HTML document.
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jun 27 '08 #3
DL wrote:
On May 17, 6:52 pm, DL <tatata9...@gma il.comwrote:
>newTRc1.innerH TML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
selected\>Ye s/No\<option value="CK"\>Che ckbox\</select\><br\>';

Ok, so, it looks like I wasn't thinking, the Event handler within the
newly created element hasn't been loaded by a browser, hence, no
action upon such an event. Yes?
Not quite. The event handler exists before, as native code. The event
handler attribute value is a string of characters that is passed on to the
script engine which uses it to generate a function (the primary event
listener) that is called by the event handler on event.

However, as the generated code lacks the `onchange' attribute due to missing
separation of the relevant HTML code from the rest here, there is nothing to
pass on to the script engine, and no primary event listener is being created
that could be called on event.
Or another better approach to achieve the same goal?
See my other followup.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #4
Thomas 'PointedEars' Lahn wrote:
DL wrote:
>On May 17, 6:52 pm, DL <tatata9...@gma il.comwrote:
>>newTRc1.inner HTML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
selected\>Y es/No\<option value="CK"\>Che ckbox\</select\><br\>';
Ok, so, it looks like I wasn't thinking, the Event handler within the
newly created element hasn't been loaded by a browser, hence, no
action upon such an event. Yes?

Not quite. The event handler exists before, as native code. The event
handler attribute value is a string of characters that is passed on to the
script engine which uses it to generate a function (the primary event
^^^^^^^^^^^
"to create" is the better, less confusing verb here.
listener) that is called by the event handler on event.

PointedEars
Jun 27 '08 #5
DL
On May 17, 9:08*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
DL wrote:
What I wanted to do is to call a function from a newly created
element. * But it stumbled me.
Here's the line that references the newly created element and I used
the alert function for debugging for now. * Did I mess up all these
quotes?

Yes, you did.
// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:

* * * * * * * * * * *^start * * * * * * * * *end^ * ^start\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"

* * * * * * * * * end^ * *^start[1] * * * ^^^[2]selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end

There. *Because you have not properly delimited all your attribute values
[^1], you will generate

* <select name=qType42onc hange="alert(i) ;">

if i == 42. *What do you expect of the user agent to do here?

While delimiting all attribute values with " --

* <select name="qType42"o nchange="alert( i);">

-- or including the missing whitespace --

* <select name=qType42 onchange="alert (i);">

-- or do both of them would be a quick fix, I strongly suggest you stop
cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator
and mutator methods instead. *With a few user-defined wrappers they can be
even more efficient to use than what you have now.

http://www.w3.org/TR/DOM-Level-2-Core/

Furthermore, if you generate the identifier of `i' instead of the current
value of `i' [^2], it will be resolved on execution of the event handler
attribute value, which is probably not what you want here.

BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO)
should be escaped as `<\/' if the source code is within an HTML document.
Ok, many thanks. Now, I changed part of it to
... <select name=qType'+i+' onchange="doChe ckbox('+i+')">< option ...

This event handler is now recognized by my browser (IE7), however, I'm
getting "Object expected" error, fyi, the doCheckbox function (as a
sub function) looks this:

function doCheckbox(i) {
// debug
alert(i);
// add a new checkbox here
/* ... */
}

Jun 27 '08 #6
DL
On May 17, 9:47*pm, DL <tatata9...@gma il.comwrote:
On May 17, 9:08*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:


DL wrote:
What I wanted to do is to call a function from a newly created
element. * But it stumbled me.
Here's the line that references the newly created element and I used
the alert function for debugging for now. * Did I mess up all these
quotes?
Yes, you did.
// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
* * * * * * * * * * *^start * * * * * * * * *end^ * ^start\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
* * * * * * * * * end^ * *^start[1] * * * ^^^[2]selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end
There. *Because you have not properly delimited all your attribute values
[^1], you will generate
* <select name=qType42onc hange="alert(i) ;">
if i == 42. *What do you expect of the user agent to do here?
While delimiting all attribute values with " --
* <select name="qType42"o nchange="alert( i);">
-- or including the missing whitespace --
* <select name=qType42 onchange="alert (i);">
-- or do both of them would be a quick fix, I strongly suggest you stop
cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator
and mutator methods instead. *With a few user-defined wrappers they can be
even more efficient to use than what you have now.
http://www.w3.org/TR/DOM-Level-2-Core/
Furthermore, if you generate the identifier of `i' instead of the current
value of `i' [^2], it will be resolved on execution of the event handler
attribute value, which is probably not what you want here.
BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO)
should be escaped as `<\/' if the source code is within an HTML document..

Ok, many thanks. *Now, I changed part of it to
... <select name=qType'+i+' onchange="doChe ckbox('+i+')">< option ...

This event handler is now recognized by my browser (IE7), however, I'm
getting "Object expected" error, fyi, the doCheckbox function (as a
sub function) looks this:

function doCheckbox(i) {
* // debug
* *alert(i);
* // add a new checkbox here
* /* ... */

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
ok, I've fixed this problem by making the doCheckbox() a separate
function . Now,
with the doCheckbox() function,

If my code looks like the following,
if (document.getEl ementById('qTyp e'+i).value = 'CK') {
var TBL = document.getEle mentById('tbl') ;
...
}
I got "document.getEl ementById(...)" is null or an object err but if I
comment out the IF line
if (document.getEl ementById('qTyp e'+i).value = 'CK')
then the code works but I lose the logic of adding the checkbox only
when type is checkbox. Why?

Thanks.
Jun 27 '08 #7
DL
On May 17, 10:35*pm, DL <tatata9...@gma il.comwrote:
On May 17, 9:47*pm, DL <tatata9...@gma il.comwrote:


On May 17, 9:08*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
DL wrote:
What I wanted to do is to call a function from a newly created
element. * But it stumbled me.
Here's the line that references the newly created element and I used
the alert function for debugging for now. * Did I mess up all these
quotes?
Yes, you did.
// once again this is the key line of code, problem area, disregard
var i etc...
newTRc1.innerHT ML ='\<input type="text" name=q'+i+' size="60"\>;Typ e:
* * * * * * * * * * *^start * * * * * * * * *end^ * ^start\<select name=qType'+i+ 'onchange="aler t(i);"\>\<optio n value="YN"
* * * * * * * * * end^ * *^start[1] * * * ^^^[2]selected\>Yes/No\<option value="CK"\>Che ckbox\</select\><br\>';
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *^end
There. *Because you have not properly delimited all your attribute values
[^1], you will generate
* <select name=qType42onc hange="alert(i) ;">
if i == 42. *What do you expect of the user agent to do here?
While delimiting all attribute values with " --
* <select name="qType42"o nchange="alert( i);">
-- or including the missing whitespace --
* <select name=qType42 onchange="alert (i);">
-- or do both of them would be a quick fix, I strongly suggest you stop
cooking proprietary (innerHTML) tag soup and start using DOM Level 2 creator
and mutator methods instead. *With a few user-defined wrappers they can be
even more efficient to use than what you have now.
>http://www.w3.org/TR/DOM-Level-2-Core/
Furthermore, if you generate the identifier of `i' instead of the current
value of `i' [^2], it will be resolved on execution of the event handler
attribute value, which is probably not what you want here.
BTW, `<' and `>' do not need to be escaped in string literals; `</' (ETAGO)
should be escaped as `<\/' if the source code is within an HTML document.
Ok, many thanks. *Now, I changed part of it to
... <select name=qType'+i+' onchange="doChe ckbox('+i+')">< option ...
This event handler is now recognized by my browser (IE7), however, I'm
getting "Object expected" error, fyi, the doCheckbox function (as a
sub function) looks this:
function doCheckbox(i) {
* // debug
* *alert(i);
* // add a new checkbox here
* /* ... */
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

ok, I've fixed this problem by making the doCheckbox() a separate
function . *Now,
with the doCheckbox() function,

If my code looks like the following,
if (document.getEl ementById('qTyp e'+i).value = 'CK') {
* *var TBL = document.getEle mentById('tbl') ;
* *...}

I got "document.getEl ementById(...)" is null or an object err but if I
comment out the IF line
if (document.getEl ementById('qTyp e'+i).value = 'CK')
then the code works but I lose the logic of adding the checkbox only
when type is checkbox. *Why?

Thanks.- Hide quoted text -
Ahe, now I think I know why the above IF statement failed because it's
not a simple value, the SELECT element may multiple values...
Jun 27 '08 #8
DL wrote:
On May 17, 10:35 pm, DL <tatata9...@gma il.comwrote:
>If my code looks like the following,
if (document.getEl ementById('qTyp e'+i).value = 'CK') {
var TBL = document.getEle mentById('tbl') ;
...}

I got "document.getEl ementById(...)" is null or an object err but if I
^^^^^^^^^^^^
"... or _not_ an object"
>comment out the IF line
if (document.getEl ementById('qTyp e'+i).value = 'CK')
then the code works but I lose the logic of adding the checkbox only
when type is checkbox. Why?

Ahe, now I think I know why the above IF statement failed because it's
not a simple value, the SELECT element may multiple values...
No, it is because document.getEle mentById('qType '+i) cannot be evaluated to
an object reference, as the error message says. So either there exists no
element with that ID (yet), or your markup is not Valid.

You have also assigned a value (`=') when you wanted a comparison (`==' or
`===').

BTW, full-quoting hundreds of unnecessary lines is not going to encourage
people to read your postings, much less to reply to them.

http://jibbering.com/faq/
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #9
DL
On May 18, 7:29*am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
>
comment out the IF line
if (document.getEl ementById('qTyp e'+i).value = 'CK')
then the code works but I lose the logic of adding the checkbox only
when type is checkbox. *Why?
Ahe, now I think I know why the above IF statement failed because it's
not a simple value, the SELECT element may multiple values...

No, it is because document.getEle mentById('qType '+i) cannot be evaluated to
an object reference, as the error message says. *So either there exists no
element with that ID (yet), or your markup is not Valid.

You have also assigned a value (`=') when you wanted a comparison (`==' or
`===').

BTW, full-quoting hundreds of unnecessary lines is not going to encourage
people to read your postings, much less to reply to them.

http://jibbering.com/faq/
Thanks a lot. So, how do I reference a dynamic ID? Let's continue to
use this case, the following line attempts to put a string literal
inside a simgle quotes then add the var i value then single quote the
whole "thing", won't work
document.getEle mentById(''qTyp e'+i+'').value

Jun 27 '08 #10

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

Similar topics

1
4967
by: Ryan Stewart | last post by:
If you don't want to read this post because of its length, I understand. I've spent two and a half days on this problem and have a good deal of information to relate. And this is kind of a long shot, but I'm just hoping someone here has experienced a similar problem and has a better idea of what's going on than I do. First, I've tested this in IE 6.0, Netscape 7.2, Mozilla 1.75, and Firefox 1.0. It works fine in IE (even though I was...
4
2506
by: thomastk | last post by:
Hi, In the following script, I am trying to set selection to a select option element, that is newly created within the script. It works fine on IE installations on Windows 2000 and some XP machines. But on some XP machines, the selection doesn't happen and it defaults to the first element in the options array. Has anybody come across this problem ? Any known workarounds? Thanks Thomas.
5
4642
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
10
3256
by: jaxent | last post by:
Does anyone know why IE gives the error "'document.myForm.dummy' is null or not an object" on the following page? Firefox does what I expect, creates a text box and writes "hello" in it. <html> <head> <SCRIPT type="text/javascript"/"> function createForm(){ var divEle = document.getElementById("putHere");
1
3211
by: mikejr83 | last post by:
I've been working on a project converting some IE code to be cross-browser complient. I've run into a little problem figuring out how to migrate the IE specific mergeAttributes function to something similar that is complient in NS/Mozilla/Firefox browsers. Currently I have this basic code row = this.insertRow(position) //I'm inserting a row into "this" table row.mergeAttributes(this.templateRow); //here I'm basically "copying" this...
28
4333
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
8
1679
by: dd | last post by:
Hi, I've discovered a scenario where Safari 1.3 (I need to make my stuff compliant with 1.3+) gets confused about the scope of local variables WITHIN functions that were created in dynamic script blocks. I've made this example where function def has a local i variable in a loop, and it calls function abc which also has a local i variable in a loop. What happens is that Safari is not respecting the scope and is allowing the called...
5
4290
by: bizt | last post by:
Hi, Below I have a simple object / function thing (still getting head round these) declaration: function MyObject() { this.alertMe = function() { alert('hello'); }; this.alertMeAgain() {
3
4223
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row value (text value) here is the code if possible please help me Thanks beforehand <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0
8987
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
8826
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
9366
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...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
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
6793
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
6073
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
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2211
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.