Generating HTML

Question: I like to know whether JavaScript can create input element such as text, button or drop down list dynamically?
Chester Heng

Answer: The correct way of creating HTML from JavaScript is to make use of the part of JavaScript called the Document Object Model (or DOM).

JavaScript has several commands for creating the pieces of the content that are to be added to the web page including createElement() for creating HTML tags and createTextNode() for the text that you want within an HTML tag. The appendChild() command can then be used to attach these components together and either that or the insertBefore() command can be used to add the new content into the page.

The elements for a form are created in the same way as any other HTML content however there is a quicker way of creating the options in a select list by making use of the Option object.

These pages on the Document Object Model provide more detailed information on interacting with HTML in general and on interacting with forms.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate