By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

is dynamically creating a new field on a page DHTML, Javascript, XML or a combination?

P: n/a
I want to learn moer of what I saw in a recent example. They create a page
that created new fields/element. It's not like they were hidden and they
displayed them, they were not there, then the script created them.

It used things like parentnode, insertBefore, appendChildNode,...

Any ideas/direction is appreciated.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"NotGiven" <no****@nonegiven.net> wrote in message
news:V1********************@bignews1.bellsouth.net ...
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they
displayed them, they were not there, then the script created them.

It used things like parentnode, insertBefore, appendChildNode,...

Any ideas/direction is appreciated.


The answer is Yes!

DHTML (Dynamic Html) is just using Javascript to manipulate
the (html) page dynamically, and the common way to do this,
is to manipulate the pages Document Object Model (DOM)'s
parentnode, insertBefore, appendChildNode, et.c...

Xml is not (usually) used for such things, but with the right
xml style sheet (XSL/XSLT), you can create dynamic HTML-documents
from xml-documents too...

--
Dag.
Jul 23 '05 #2

P: n/a
So if I want to search for web sites to learn more about this typ eof thing,
I would search for DHTML?

"Dag Sunde" <ds@orion.no-way> wrote in message
news:tu******************@juliett.dax.net...
"NotGiven" <no****@nonegiven.net> wrote in message
news:V1********************@bignews1.bellsouth.net ...
I want to learn moer of what I saw in a recent example. They create a

page
that created new fields/element. It's not like they were hidden and they
displayed them, they were not there, then the script created them.

It used things like parentnode, insertBefore, appendChildNode,...

Any ideas/direction is appreciated.


The answer is Yes!

DHTML (Dynamic Html) is just using Javascript to manipulate
the (html) page dynamically, and the common way to do this,
is to manipulate the pages Document Object Model (DOM)'s
parentnode, insertBefore, appendChildNode, et.c...

Xml is not (usually) used for such things, but with the right
xml style sheet (XSL/XSLT), you can create dynamic HTML-documents
from xml-documents too...

--
Dag.

Jul 23 '05 #3

P: n/a
"NotGiven" <no****@nonegiven.net> wrote in message
news:Q6********************@bignews1.bellsouth.net ...
"Dag Sunde" <ds@orion.no-way> wrote in message
news:tu******************@juliett.dax.net...
"NotGiven" <no****@nonegiven.net> wrote in message
news:V1********************@bignews1.bellsouth.net ...
I want to learn moer of what I saw in a recent example. They create a page
that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the script created them.

It used things like parentnode, insertBefore, appendChildNode,...

Any ideas/direction is appreciated.


The answer is Yes!

DHTML (Dynamic Html) is just using Javascript to manipulate
the (html) page dynamically, and the common way to do this,
is to manipulate the pages Document Object Model (DOM)'s
parentnode, insertBefore, appendChildNode, et.c...

Xml is not (usually) used for such things, but with the right
xml style sheet (XSL/XSLT), you can create dynamic HTML-documents
from xml-documents too...


So if I want to search for web sites to learn more about this typ eof

thing, I would search for DHTML?


Yes...
....And Javascript, and post specific questions here when you're stuck... ;-)
--
Dag
5826'15.9" N 00846'45.5" E
Jul 23 '05 #4

P: n/a
I posted one here a few days ago and no one replied. I thought I might in
the wrong newsgroup. Here's what I posted:

===========================================

The code below is overwriting itself.

What it is doing is creating the fields correctly the first time through.
Then it writes over those same fields again and again. I can tell because
the value of the first field changes every loop.

Any ideas?

Code:
var ex_counter = 0;
var ex = "";
function ex_morefields() {
var x=document.getElementById("choices");
ex_counter++;
var newfields = document.getElementById('ex_fields').cloneNode(tru e);
newfields.id = '';
newfields.style.display = 'block';
var newfield = newfields.childNodes;
for (y = 0; y<(x.length); y++) { //loop through all item in the list
ex = x[y].text;
for (var i=0;i<newfield.length;i++) {
var theName = newfield[i].name;
if (theName) {
newfield[i].name = theName;// + ex_counter;
}
if (theName == "exercise") {
newfield[i].setAttribute('value', ex);
}
}
var insertHere = document.getElementById('write_EX');
insertHere.parentNode.insertBefore(newfields,inser tHere);
}
} // end add more software script
here's the html in the page:

HTML:
<span id="write_EX"></span>
Jul 23 '05 #5

P: n/a
On Fri, 17 Sep 2004 14:36:00 -0400, NotGiven <no****@nonegiven.net> wrote:
I posted one here a few days ago and no one replied.
You posted it yesterday. At a quick glance, I couldn't see what was wrong,
but after reformatting your code, it's quite clear, so in future please
indent code blocks. It's much easier to follow code that way. In addition,
post *all* the relevant HTML. You reference three elements by id, and many
more through the childNodes collection. How can we tell what's being
manipulated without the document?

[snip]
The code below is overwriting itself.
With very good reason. However, you might also like to know that it will
probably fail in most browsers, if you're doing what I think you're doing.
What it is doing is creating the fields correctly the first time through.
Then it writes over those same fields again and again. I can tell because
the value of the first field changes every loop.
That may be so, but you always insert the same node, which results in it
being removed, then being reinserted.
var ex_counter = 0;
var ex = "";
Why is 'ex' global?
function ex_morefields() {
var x=document.getElementById("choices");
ex_counter++;
var newfields = document.getElementById('ex_fields').cloneNode(tru e);
newfields.id = '';
newfields.style.display = 'block';
var newfield = newfields.childNodes;
for (y = 0; y<(x.length); y++) { //loop through all item in the list
What list?

This is why I said this code will fail. A conforming browser will only
return one node, or nothing at all, with a getElementById call. This means
that the length property will be undefined and the loop will never be
entered. You seem to be under the impression that it will return a
collection which implies a few things:

1) You've only tested on IE, which is far from a compliant browser.
2) You're using the same id on several elements.
3) You haven't validated you page before trying to script it.

The DOM Specification doesn't define specific behaviour with a document
contains elements with identical id attributes and a getElementById call.
This means that only the first element in document order might be
returned. It's even reasonable to assume that null can be returned. What
IE does - return a collection - is certainly not correct as the interface
for the method clearly defines what can be returned, and a NodeList is not
one of the options.

It's advisable to test on a decent browser before checking with IE. You're
more likely to find bugs that way. Opera and Mozilla are good options, and
are available on many platforms. Also validate:
<URL:http://validator.w3.org/>.
ex = x[y].text;
for (var i=0;i<newfield.length;i++) {
var theName = newfield[i].name;
if (theName) {
newfield[i].name = theName;// + ex_counter;
Rather redundant, isn't it?
}
if (theName == "exercise") {
newfield[i].setAttribute('value', ex);
Use of setAttribute is usually unnecessary. If the element defines the
attribute as a property, as in the case of INPUT elements and the value
attribute, then

<object>.<property> = <value>;

is all that's required.
}
}
var insertHere = document.getElementById('write_EX');
Why do you repeatedly look up this reference?
insertHere.parentNode.insertBefore(newfields,inser tHere);
This is the other issue. The newfields variable is only defined once
outside the outer loop, so it never changes. You probably meant
newfield[i], but the first problem is the most pressing as correcting it
might require you to re-examine all of this code, and probably the HTML,
too.
}
} // end add more software script


One final point: In all of the code above, you fail to check if the user
agent supports the methods and objects you use. Always feature test (4.26).

<URL:http://jibbering.com/faq/>

[snip]

I'm sorry if that's a lot of information to throw at you, but it's all
important. If you need more help, you'll have to post your HTML and what
you're trying to accomplish. This would be best done by providing a link
to a demonstration.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

P: n/a
I would like nothing more than to show you the entire page but it is
proprietary at the moment. I'll work on making it more generic then post
it.

Thanks for your expert help. I've been developing for year but in another
language and have little javascript exp other than copying code form
tutorials and maiking it work on my site.

This code is more advanced than I have a prayer of solving at this point.

My goal is to create fields on the fly. This page has a listbox with
several items. I'd like to click a button that will create fields on the
fly that have the listbox items as values in the fields.

What do you suggest I read to learn enough to do this?

Thanks.
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsehlpsx5x13kvk@atlantis...
On Fri, 17 Sep 2004 14:36:00 -0400, NotGiven <no****@nonegiven.net> wrote:
I posted one here a few days ago and no one replied.


You posted it yesterday. At a quick glance, I couldn't see what was wrong,
but after reformatting your code, it's quite clear, so in future please
indent code blocks. It's much easier to follow code that way. In addition,
post *all* the relevant HTML. You reference three elements by id, and many
more through the childNodes collection. How can we tell what's being
manipulated without the document?

[snip]
The code below is overwriting itself.


With very good reason. However, you might also like to know that it will
probably fail in most browsers, if you're doing what I think you're doing.
What it is doing is creating the fields correctly the first time through.
Then it writes over those same fields again and again. I can tell because
the value of the first field changes every loop.


That may be so, but you always insert the same node, which results in it
being removed, then being reinserted.
var ex_counter = 0;
var ex = "";


Why is 'ex' global?
function ex_morefields() {
var x=document.getElementById("choices");
ex_counter++;
var newfields = document.getElementById('ex_fields').cloneNode(tru e);
newfields.id = '';
newfields.style.display = 'block';
var newfield = newfields.childNodes;
for (y = 0; y<(x.length); y++) { //loop through all item in the list


What list?

This is why I said this code will fail. A conforming browser will only
return one node, or nothing at all, with a getElementById call. This means
that the length property will be undefined and the loop will never be
entered. You seem to be under the impression that it will return a
collection which implies a few things:

1) You've only tested on IE, which is far from a compliant browser.
2) You're using the same id on several elements.
3) You haven't validated you page before trying to script it.

The DOM Specification doesn't define specific behaviour with a document
contains elements with identical id attributes and a getElementById call.
This means that only the first element in document order might be
returned. It's even reasonable to assume that null can be returned. What
IE does - return a collection - is certainly not correct as the interface
for the method clearly defines what can be returned, and a NodeList is not
one of the options.

It's advisable to test on a decent browser before checking with IE. You're
more likely to find bugs that way. Opera and Mozilla are good options, and
are available on many platforms. Also validate:
<URL:http://validator.w3.org/>.
ex = x[y].text;
for (var i=0;i<newfield.length;i++) {
var theName = newfield[i].name;
if (theName) {
newfield[i].name = theName;// + ex_counter;


Rather redundant, isn't it?
}
if (theName == "exercise") {
newfield[i].setAttribute('value', ex);


Use of setAttribute is usually unnecessary. If the element defines the
attribute as a property, as in the case of INPUT elements and the value
attribute, then

<object>.<property> = <value>;

is all that's required.
}
}
var insertHere = document.getElementById('write_EX');


Why do you repeatedly look up this reference?
insertHere.parentNode.insertBefore(newfields,inser tHere);


This is the other issue. The newfields variable is only defined once
outside the outer loop, so it never changes. You probably meant
newfield[i], but the first problem is the most pressing as correcting it
might require you to re-examine all of this code, and probably the HTML,
too.
}
} // end add more software script


One final point: In all of the code above, you fail to check if the user
agent supports the methods and objects you use. Always feature test
(4.26).

<URL:http://jibbering.com/faq/>

[snip]

I'm sorry if that's a lot of information to throw at you, but it's all
important. If you need more help, you'll have to post your HTML and what
you're trying to accomplish. This would be best done by providing a link
to a demonstration.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.