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

DIV PROBLEM

P: n/a
Hi guys this is my problem :

I have created a js function where I can add fields on the fly, but
there seems to be a small flaw with Mozilla.

If you try to click on "Add" button you can see that for every click
there is a new text field and this is really useful
because I don't need to refresh the page every time. It works 100% fine
with Internet Explorer but in Mozilla it seems to loose the values
I type in in field 2 or field 3 etc.

For example, if I have 2 lines and I click to get a third one, I loose
the values in the fields in line 2, and line 1.

If I have 3 lines and want to add another one, I loose the values for
lines 2 and 3, and 1.

Again, this works fine in IE, but not in Mozilla.

Any idea how to fix this?
*this is the page *
http://dev.shipco.com/temp/div.html
*this is the code*

<form Action ="Save.php?ID=106" Name="input1" Method = Post
Onsubmit="return Checkform(this);">
<div id="prova"></div>

<table width=100% border="0" align="center" cellpadding="3"
cellspacing="1" >

<script>
function htmlwrite()
buildHTML= "<input type=text><br>"
elem = document.getElementById('prova');
elem.innerHTML = elem.innerHTML + buildHTML;

}
</script>
</TD></TR>
<tr>
<td align=center colspan=2>
<input type=button class=button name=add
onClick="javascript:htmlwrite();" value = "Add TEXT FIELD">
</td>
</tr>
<Table>

Thanks a lot guys

Andrea

Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
First, do not crosspost over top hierarchies. X-Post removed.

Second, do not try to arouse interest in your postings by writing the
Subject only in uppercase letters and/or with multiple sentence marks.
More people will be repelled than attracted by such a Subject header.

Third:

| [03:29:31] pointedears:~ $ host -t mx kkjkjkjij.it
| Note: nslookup is deprecated and may be removed from future releases.
| Consider using the `dig' or `host' programs instead. Run nslookup
| with the `-sil[ent]' option to prevent this message from appearing.
| Server: 62.53.246.30
| Address: 62.53.246.30#53
|
| ** server can't find kkjkjkjij.it: NXDOMAIN

By using a faked e-mail address you prevent you from getting
spam, but at the same time you help to destroy the Internet.
Due to the egotistical behavior you display with such, I will
not download your postings anymore until you change that. See
<http://www.interhack.net/pubs/munging-harmful/> for details.

Andrea Gelsogroove wrote:
http://dev.shipco.com/temp/div.html
http://validator.w3.org/check?uri=ht.../temp/div.html
<form Action ="Save.php?ID=106" Name="input1" Method = Post
Onsubmit="return Checkform(this);">
<form action="Save.php?ID=106" name="input" method="post"
onsubmit="return Checkform(this);"

Watch for the case in URIs. Is the filename really `Save.php' or
is it `save.php' instead? Most servers are not using FAT or NTFS
file systems, thus filename case is important.

Watch for the case in attribute and JavaScript identifiers, too.
Lowercased identifiers are not only better compressible, they
also save you trouble in case-sensitive applications like XHTML
and JavaScript. For JavaScript, only identifiers of constructor
functions should start with an uppercase character, to avoid
confusion.
<div id="prova"></div>

<table width=100% border="0" align="center" cellpadding="3"
cellspacing="1" >

<script>
The above is all invalid HTML. Write at least

<table width="100%" border="0" align="center" cellpadding="3"
cellspacing="1">

and

<script type="text/javascript">

(But `script' *must* *not* be a child of `table', see below.)
function htmlwrite()
buildHTML= "<input type=text><br>"
ŽbuildHTML' is unnecessarily declared global here.
Use the `var' keyword to make it a local variable.

`type="text"' is the default, you can safely omit it.
elem = document.getElementById('prova');
Again, `elem' need not and should not be declared global *in*
*general*. However, for this is a method called more than once
and operating on the same object, `elem' should be global and
hold the reference to the HTMLDivElement Object so that it
should not be retrieved every time the function is called.

But more important, document.getElementById(...) is a method
of the W3C-DOM and you do not check for support of it before
accessing it. See

http://pointedears.de.vu/scripts/test/whatami

for details.
elem.innerHTML = elem.innerHTML + buildHTML;
OTOH `innerHTML' is a proprietary property *not* part of the
W3C-DOM. Although supported in the HTML-DOM of recent user
agents, you should stick to one object model because of the
reasons mentioned in the test case document and because of the
problems that will arise otherwise if you possibly change the
DOCTYPE to XHTML.

Quickhack:

var oProva = null; // was `elem' previously
if (document.getElementById) // possibly W3C-DOM compliant
oProva = document.getElementById('prova');
else if (document.all) // possibly IE4-DOM compliant
oProva = document.all['prova'];
// Using the argument `o' for passing the reference
// makes the function more independent of the context
// it is called from.
function htmlWrite(o)
{
if (o)
{
if (document.createElement && o.appendChild)
{
var oInput = document.createElement("input");
if (oInput)
{
oInput.type = "text";
if (o.appendChild(oInput))
{
var oBr = document.createElement("br");
if (oBr)
{
o.appendChild(oBr);
}
}
}
}
else if (typeof o.innerHTML != "undefined")
{
o.innerHTML += "<input><br>\n";
}
}
}
}
</script>
</TD></TR>
That is like my recent example of how `script' elements can be misplaced
while creating invalid HTML. The `script' element must not be the child
element of the `table' element, only `tbody', `thead', `tfoot', `th' and
`td' may be. The `script' element, however, has nothing to do with the
`table' element, thus it should be the child of the `head' element so
that the function is early available.
<tr>
<td align=center colspan=2>
<input type=button class=button name=add
onClick="javascript:htmlwrite();" value = "Add TEXT FIELD">
Once you define the default scripting language standards-compliant with

<meta http-equiv="Content-Script-type" content="text/javascript">

within the `head' element you do not longer require proprietary
extensions like this. `javascript:' is basically a proprietary URI
scheme. Using it in presumed JavaScript code makes it only a label.
Only the IE browser component reads this label to define the used
scripting language, other UAs do not and will even trigger script
errors if labels are not supported (in event handlers). Write

<input type="button" name="add" value="Add TEXT FIELD" class="button"
onclick="htmlWrite(oProva);"> <!-- pass a reference as arg. -->

instead. Note that users without JavaScript cannot use that
button, so you should provide a working alternative for them.
</td>
</tr>
<Table>


The last tag should be the close tag </table>. But you will not
want nor need a table here. Never use a `table' element for layout
purposes only. A table is a table is a table [psf 3.8], and CSS
exists.
HTH

PointedEars
Jul 20 '05 #2

P: n/a
Thomas 'PointedEars' Lahn hu kiteb:
By using a faked e-mail address you prevent you from getting
spam, but at the same time you help to destroy the Internet.
Due to the egotistical behavior you display with such, I will
not download your postings anymore until you change that. See
<http://www.interhack.net/pubs/munging-harmful/> for details.


Just out of curiousity, which do you consider more harmful to teh
Internet:

1 - An obviously invalid/munged email address.
2 - An email address which looks valid, but which is never read.

With the first, anyone who tries and has basic knowledge of the internet
would know it is invalid. With the second, they will never know, and
neither would teh intended recipient, unless they then mention it in the
newsgroup.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.