jQuery: creating new tags

Programming pages

We’ve seen that we can select existing elements on a web page in many ways using the $(selectorsyntax, where ‘selector’ can take on any of the many forms we’ve seen in earlier posts. The $() syntax can also be used to create new HTML which can then be inserted into your document.

To do this, simply put the desired HTML (enclosed in quotes) directly as the argument. Thus $(‘<input type=text/>’) creates a new text input box.

Merely creating a new object does not, however, insert it into the document. To do that, you need to use a method such as appendTo(). Thus  $(‘<input type=text/>’).appendTo(‘body’) will add the text box to the end of the body, so that it appears at the bottom of the page.

The easiest way of trying this feature out is to load a page (such as the one we’ve been using…

View original post 278 more words