Connecting Tech Pros Worldwide Forums | Help | Site Map

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

NotGiven
Guest
 
Posts: n/a
#1: Jul 23 '05
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.



Dag Sunde
Guest
 
Posts: n/a
#2: Jul 23 '05

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


"NotGiven" <noname@nonegiven.net> wrote in message
news:V1E2d.150662$0o5.36812@bignews1.bellsouth.net ...[color=blue]
> I want to learn moer of what I saw in a recent example. They create a[/color]
page[color=blue]
> 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.
>[/color]

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.


NotGiven
Guest
 
Posts: n/a
#3: Jul 23 '05

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


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:tuE2d.2455$8c.200479@juliett.dax.net...[color=blue]
> "NotGiven" <noname@nonegiven.net> wrote in message
> news:V1E2d.150662$0o5.36812@bignews1.bellsouth.net ...[color=green]
>> I want to learn moer of what I saw in a recent example. They create a[/color]
> page[color=green]
>> 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.
>>[/color]
>
> 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.
>
>[/color]


Dag Sunde
Guest
 
Posts: n/a
#4: Jul 23 '05

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


"NotGiven" <noname@nonegiven.net> wrote in message
news:Q6F2d.150985$0o5.76694@bignews1.bellsouth.net ...[color=blue]
> "Dag Sunde" <ds@orion.no-way> wrote in message
> news:tuE2d.2455$8c.200479@juliett.dax.net...[color=green]
> > "NotGiven" <noname@nonegiven.net> wrote in message
> > news:V1E2d.150662$0o5.36812@bignews1.bellsouth.net ...[color=darkred]
> >> I want to learn moer of what I saw in a recent example. They create a[/color]
> > page[color=darkred]
> >> that created new fields/element. It's not like they were hidden and[/color][/color][/color]
they[color=blue][color=green][color=darkred]
> >> displayed them, they were not there, then the script created them.
> >>
> >> It used things like parentnode, insertBefore, appendChildNode,...
> >>
> >> Any ideas/direction is appreciated.
> >>[/color]
> >
> > 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...
> >[/color]
>
> So if I want to search for web sites to learn more about this typ eof[/color]
thing,[color=blue]
> I would search for DHTML?
>[/color]

Yes...
....And Javascript, and post specific questions here when you're stuck... ;-)


--
Dag
58°26'15.9" N 008°46'45.5" E


NotGiven
Guest
 
Posts: n/a
#5: Jul 23 '05

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


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>


Michael Winter
Guest
 
Posts: n/a
#6: Jul 23 '05

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


On Fri, 17 Sep 2004 14:36:00 -0400, NotGiven <noname@nonegiven.net> wrote:
[color=blue]
> I posted one here a few days ago and no one replied.[/color]

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]
[color=blue]
> The code below is overwriting itself.[/color]

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.
[color=blue]
> 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.[/color]

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

Why is 'ex' global?
[color=blue]
> 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[/color]

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/>.
[color=blue]
> 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;[/color]

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

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.
[color=blue]
> }
> }
> var insertHere = document.getElementById('write_EX');[/color]

Why do you repeatedly look up this reference?
[color=blue]
> insertHere.parentNode.insertBefore(newfields,inser tHere);[/color]

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.
[color=blue]
> }
> } // end add more software script[/color]

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.
NotGiven
Guest
 
Posts: n/a
#7: Jul 23 '05

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


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.Winter@blueyonder.co.invalid> wrote in message
news:opsehlpsx5x13kvk@atlantis...[color=blue]
> On Fri, 17 Sep 2004 14:36:00 -0400, NotGiven <noname@nonegiven.net> wrote:
>[color=green]
>> I posted one here a few days ago and no one replied.[/color]
>
> 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]
>[color=green]
>> The code below is overwriting itself.[/color]
>
> 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.
>[color=green]
>> 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.[/color]
>
> That may be so, but you always insert the same node, which results in it
> being removed, then being reinserted.
>[color=green]
>> var ex_counter = 0;
>> var ex = "";[/color]
>
> Why is 'ex' global?
>[color=green]
>> 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[/color]
>
> 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/>.
>[color=green]
>> 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;[/color]
>
> Rather redundant, isn't it?
>[color=green]
>> }
>> if (theName == "exercise") {
>> newfield[i].setAttribute('value', ex);[/color]
>
> 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.
>[color=green]
>> }
>> }
>> var insertHere = document.getElementById('write_EX');[/color]
>
> Why do you repeatedly look up this reference?
>[color=green]
>> insertHere.parentNode.insertBefore(newfields,inser tHere);[/color]
>
> 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.
>[color=green]
>> }
>> } // end add more software script[/color]
>
> 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.[/color]


Closed Thread