search
HomeWeb Front-endJS TutorialJavaScript DOM Learning Chapter 7 Form Extension_Basic Knowledge

Idea
Suppose you have an online CD rating tool. You want users to view all their favorite CDs. But how do you know how many images a user will view on average? How many fields do you need to add to this page?
This was indeed a problem before W3CDOM came along. Let's say you placed 7 CDs. But users may only want to view one CD, and too many may scare users. Some users want to view all their CDs, so they have to submit them many times. This is really annoying.
Only using W3C DOM allows the user to decide how many fields to generate. This effect is very different from the previous one.
Example
When you click Send, the form will send all the parameters obtained in the form of an array. This is used to check whether it was actually sent to the server. Unfortunately, the mac versions of IE and Safari cannot send.
Problems with IE
IE under windows has two serious problems:
The first problem is that all generated radio button boxes belong to the same array, even if their names are different. In this way, the user can only select one of all radio button boxes. In other words, you cannot use radio buttons in the generated form.
Some readers said that there is no problem with the radio button generated through innerHTML. If you insist on using radio buttons, you can try it.
The second problem is that the generated forms are not accessible through traditional document.forms: IE does not include them in the array. This can be solved by setting IDs for them.
Explain the HTML code of the
form:

Copy the code The code is as follows:









True The form item is in the DIV with ID readroot and the display value is none. This DIV is a template and cannot be modified by the user. When the user needs more forms, we copy this template and add it after the form. We load it at the beginning so that the user can see it when they open the page.
This DIV is outside the form, so when the user submits the form, the content of this template will not be submitted.
The SPAN with ID writeroot is a tag. The newly generated form is inserted in front of it.
Add form items
The following code can be used to add form items when needed:



Copy code Code As follows:

var counter = 0;
function moreFields() {
counter ;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i var theName = newField[i].name
if (theName)
newField[i].name = theName counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;

First we need a counter counter, because all form items should have unique a name. We add the counter value after the generated name. Initialize the counter:
Copy code The code is as follows:
var counter = 0;

Then is the actual function. We add 1 to the counter:
Copy code The code is as follows:

function moreFields() {
counter ;

Then copy our template, remove the ID, and set the display to block. readroot should be the only ID in the entire document, and it should be displayed for users to see after copying the template.
Copy code The code is as follows:

var newFields = document.getElementById('readroot'). cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';

We iterate over the child elements of this copy:
Copy code The code is as follows:

var newField = newFields.childNodes;
for (var i=0; i

If the child element has a name attribute, then we add the counter value to the name value to ensure its uniqueness:
Copy code The code is as follows:

var theName = newField[i].name
if (theName)
newField[i].name = theName counter;
}

Now this copy is ready for insertion. We insert it before writeroot:
Copy code The code is as follows:
var insertHere = document.getElementById( 'writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}

Then we execute it once when the page is loaded, so that when the user enters See:
Copy code The code is as follows:
window.onload = moreFields;

Remove form items
Each template copy has a remove button:
Copy code The code is as follows:
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />

Clicking this button will remove it from its parent element (DIV). The entire generated form will disappear and will not appear again.
Translation address: http://www.quirksmode.org/dom/domform.html
Please keep the following information for reprinting
Author: Bei Yu (tw: @rehawk)
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools