473,378 Members | 1,699 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,378 software developers and data experts.

DOM help

hi i have recently created a JavaScript DOM script and well for some
reason i t does not work.
can you help me find the problem?

here is the script
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<div id="New_Input">
</div>
<input type="button" value="ADD" onclick="addfield();" />
</form>
<script language="javascript" type="text/javascript">

function addfield () {
// if this a non-DOM browser, bail out
if (document.getElementById) { return }

//Get the Input placeholder (<div id="New_Input">)
var Input_div = document.getElementById("New_Input");

//Create and add the Input to the form element
Input = document.write("<input type='text' value=''/>");
Input_div.appendChild(Input);
}
</script>
</body>
</html>

Apr 24 '06 #1
8 1452
fu*************@gmail.com wrote:
hi i have recently created a JavaScript DOM script and well for some
reason i t does not work.
can you help me find the problem?
You should say what you expect to happen, and what you see.
<script language="javascript" type="text/javascript">
Don't include language="javascript"
function addfield () {
// if this a non-DOM browser, bail out
if (document.getElementById) { return }

//Get the Input placeholder (<div id="New_Input">)
var Input_div = document.getElementById("New_Input");

//Create and add the Input to the form element
Input = document.write("<input type='text' value=''/>");
Why do you do this rather than use the DOM method createElement to
create the new element?
Input_div.appendChild(Input);
}
</script>
</body>
</html>

--
Ian Collins.
Apr 24 '06 #2
thanks, well i tried both and they didn't work, but well would it make
a difference?
and why should i not add the language="javascript"?
oh and how can i delete a Element

Apr 24 '06 #3
thanks, well i tried both and they didn't work, but well would it make
a difference?
and why should i not add the language="javascript"?
oh and how can i delete a Element?

Apr 24 '06 #4
fu*************@gmail.com wrote:
thanks, well i tried both and they didn't work, but well would it make
a difference?
Please quote context and attributions in you reply, not everyone uses
google.

You have't explained "didn't work".
and why should i not add the language="javascript"?
language="javascript" is deprecated, type="text/javascript" is all that
is required.
oh and how can i delete a Element?

Use removeChild.

--
Ian Collins.
Apr 24 '06 #5

Ian Collins wrote:
fu*************@gmail.com wrote:
thanks, well i tried both and they didn't work, but well would it make
a difference?
Please quote context and attributions in you reply, not everyone uses
google.

You have't explained "didn't work".
and why should i not add the language="javascript"?


language="javascript" is deprecated, type="text/javascript" is all that
is required.
oh and how can i delete a Element?

Use removeChild.


well it still does not work

here is the update

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<div id="New_Input">
</div>
<input type="button" value="ADD" onclick="addfield();" />
</form>
<script type="text/javascript">

function addfield () {
// if this a non-DOM browser, bail out
if (document.getElementById) { return }

//Get the Input placeholder (<div id="New_Input">)
var Input_div = document.getElementById("New_Input");

//Create and add the Input to the form element
Input = document.createElement("input");
Input_div.appendChild(Input);
}
</script>
</body>
</html> --
Ian Collins.


Apr 24 '06 #6
fu*************@gmail.com wrote:

well it still does not work
Thanks for quoting.
here is the update

// if this a non-DOM browser, bail out
if (document.getElementById) { return }

Read this line again and see if it's what you realy intended :)

--
Ian Collins.
Apr 24 '06 #7
fu*************@gmail.com said on 24/04/2006 3:15 PM AEST:

Please quote what you are replying too, trim what you aren't.
thanks, well i tried both and they didn't work, but well would it make
a difference?
Did both what? What did you try? What did you expect to happen and
what actually happened? Would what make a difference?

and why should i not add the language="javascript"?
Because the language attribute has been deprecated for many years.
oh and how can i delete a Element
If 'elementRef' is a reference to an element, then:

elementRef.parentNode.removeChild(elementRef);
does the trick for me. :-)

In reply to your OP:
function addfield () {
// if this a non-DOM browser, bail out
if (document.getElementById) { return }
That has quite the reverse effect to the comment - any browser that
supports the getElementById() method will execute the return statement.

You should not presume that a browser that supports a particular method
supports any other particular method. Test the features you use, in
this case it may be appropriate to do:

if ( !document.getElementById || !document.createElement ){
// return or do something else
}

//Get the Input placeholder (<div id="New_Input">)
var Input_div = document.getElementById("New_Input");
One way to test for support for a method and that using it returned
something useful is to test it:

var inputDiv, oInput;
if ( document.getElementById
&& (inputDiv = document.getElementById("New_Input"))
&& document.createElement
&& (oInput = document.createElement('input')))
{
inputDiv.appendChild(input);
}

Now if either of the methods is not supported or the result of calling
them returns 'undefined' they will not cause an error.

For the sake of efficiency you may wish to restructure that so you test
for the required features once only when the page loads and configure
your functions accordingly.
Back to your code...
//Create and add the Input to the form element
Input = document.write("<input type='text' value=''/>");


That method of creating elements was invented by IE and is not well
supported by other browsers (if at all). It is used because IE has
issues with setting the value of the name attribute for dynamically
created (or cloned) elements. If you are only creating form controls
and don't require multiple elements with the same name, just use an ID
attribute instead.

You may wish to read this thread:

<URL:http://groups.google.co.uk/group/com...2cbda3521fab6e

As Matt Kruse reports, the name attribute appears to be set and the
control (if successful) is submitted with the form, but you can't access
the control by name and if you look at the innerHTML property of the
form, the added input has no name attribute.

A simple test case:

<script type="text/javascript">

function addOne(form){
var x = document.createElement('input');
x.name = 'foo';
x.value = 'bar';
form.appendChild(x);
}

</script>

<form action="">
<input type="button" value="Add input"
onclick="addOne(this.form)">
<input type="submit"><br>
<input type="button" value="Show form innerHTML"
onclick="alert(this.form.innerHTML);"><br>
</form>


--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 24 '06 #8
Thanks a lot i got it to work!

That's where it is on the net
http://mysitesucks.com/Test/Input_add.php

Apr 26 '06 #9

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.