473,396 Members | 2,106 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,396 software developers and data experts.

Acess Dynamically Created Elements

Hello,
I have a textbox with an empty span element (place holder) next to it.
When the user adds some text to the text box, I create a checkbox in
the empty span element. When I create the checkbox I create a unique
ID for the checkbox and associate an OnClick event with it. When I
proceede to check that checkbox, I get a javascript error stating that
the id is not defined. This is a dynamically created element, but
shouldn't the ID of that element still be added to the DOM tree for
acess via getElementByID() ?

Any ideas/solutions? More information needed?
Thanks
-Brad

Feb 27 '06 #1
6 2352
bradb wrote:
Hello,
I have a textbox with an empty span element (place holder) next to it.
When the user adds some text to the text box, I create a checkbox in
the empty span element. When I create the checkbox I create a unique
ID for the checkbox and associate an OnClick event with it. When I
proceede to check that checkbox, I get a javascript error stating that
the id is not defined.
How does the checkbox's onclick event get the id?

This is a dynamically created element, but
shouldn't the ID of that element still be added to the DOM tree for
acess via getElementByID() ?
Yes.


Any ideas/solutions? More information needed?


Yes and yes - show how you create the element and attach the onclick and
the onclick script that can't find the id.

Generally an element gets a reference to itself with 'this', so using its
ID is not necessary.
--
Rob
Feb 28 '06 #2
This is the function that gets called to create the checkbox...

function ent_source(num) {
var str = "source_"+num;
var id_field = "src_over"+num;
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField("+id_field+")'>";
document.getElementById(str).innerHTML = replace;
}

And I get an error immediately after clicking the check box saying
something like:
Error: src_over10 is not defined

Feb 28 '06 #3
bradb wrote:
This is the function that gets called to create the checkbox...

function ent_source(num) {
var str = "source_"+num;
var id_field = "src_over"+num;
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField("+id_field+")'>";
document.getElementById(str).innerHTML = replace;
}

And I get an error immediately after clicking the check box saying
something like:
Error: src_over10 is not defined
Its looking for the object or variable src_over10, you want it to pass
the string "src_over10".

So change this: var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField("+id_field+")'>";


To this:
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField(\'"+id_field+"\')'>";

Notice the two \' around the "+id_field+" , it's probably hard to see.
This will send the string "src_over10" to the updateField function,
which can be used with a getElementById.

You could also do this:
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField(this.id)'>";

Which will do the exact same thing, and save a millisecond or two.

- JS
http://www.endeavorpub.com

Feb 28 '06 #4
Thanks! Looks/Works great!

Feb 28 '06 #5
jshanman wrote:
bradb wrote:
This is the function that gets called to create the checkbox...

function ent_source(num) {
var str = "source_"+num;
var id_field = "src_over"+num;
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField("+id_field+")'>";
document.getElementById(str).innerHTML = replace;
}

And I get an error immediately after clicking the check box saying
something like:
Error: src_over10 is not defined
Its looking for the object or variable src_over10, you want it to pass
the string "src_over10".


Not necessarily. It might be that the OP is using the deprecated IE-based
method of accessing element objects by their respective property of the
Global Object (or the object that comes before it in the scope chain) in
the event handler attribute value.
So change this:
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField("+id_field+")'>";


To this:
var replace = "(Source Over?)<input type=checkbox id='"+id_field+"'
onClick='updateField(\'"+id_field+"\')'>";

Notice the two \' around the "+id_field+" , it's probably hard to see.
This will send the string "src_over10" to the updateField function,
which can be used with a getElementById.


No, it won't. One important aspect of SGML-based attribute value delimiters
is that they must not occur unescaped (in _markup_ terms) in the attribute
value, else the attribute value is considered to end there. Here the
attribute value delimiter is the apostrophe or single quote ('), therefore
it must not occur in the attribute value.

The markup parser does not care about the ECMAScript string literal notation
that might have been used to generate it because it never sees that code,
including the escape sequence "\'". It only sees "'", and therefore (if we
assume that num === 10)

<input type=checkbox id='"src_over10' onClick='updateField('src_over10')'>
^ ^
The trailing "src_over10')'" may be ignored as an unrecognized attribute
value by the markup parser instead of causing an error. The recognized
attribute value, 'updateField(', is a syntax error for the script engine
to which this is passed, though.

Therefore, one correct variant is:

var replace = "(Source Over?)<input type='checkbox' id='" + id_field
+ "' onClick='updateField(\"" + id_field + "\")'>";

(it helps to pretty-print code ;-)) which generated

...<input type='checkbox' id='src_over10'
onClick='updateField("src_over10")'>

in that case (watch for word wrap).

However, the target element of an event can be referred to with `this':

var replace = '(Source Over?)<input type="checkbox"'
+ ' onclick="updateField(this);">';

which generated

...<input type="checkbox" onclick="updateField(this);">

always.

You will observe that the generated checkbox does not need an ID at all
if object references are passed to and used directly in the updateField()
method, and the checkbox form control is not referred elsewhere.

The generated '(Source Over?)' substring is... strange, BTW.
PointedEars
Feb 28 '06 #6
bradb wrote:
Thanks! Looks/Works great!


Only by chance.
PointedEars
Feb 28 '06 #7

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

Similar topics

2
by: Brad Esclavon | last post by:
i am creating a page that creates 10-45 text input areas dynamically in a for loop. all the fields will be identical except for content, and i need a button to clear each corresponding field...
1
by: Will | last post by:
Hi, I have a problem trying to validate dynamically created html form elements using javascript. I have dynamically created a check box using ASP for each record in a recordset and have given each...
1
by: engwar | last post by:
Not really sure how to ask this. I'm new to CSS/DHTML I want to create something similar to how yahoo and gmail handle the "to" field when you type an email. If you start typing "d" you will...
13
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
2
by: Ed Jay | last post by:
I'm dynamically creating several form input elements: mValue = integer constant; for(var j = 0; j < mValue; j++) { target = "imgCn"+ j; eName = "myFile"; eName = eName+jj;...
2
by: gubbachchi | last post by:
Hi, I have a form whose elements are created dynamically on selection, i.e. the form has only text boxes and the number of text boxes depends on the users selection, if user selects 3 then 3...
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:
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...
0
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...
0
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...
0
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,...

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.