Home > Web Front-end > JS Tutorial > jQuery Dynamically Add Form Elements

jQuery Dynamically Add Form Elements

Lisa Kudrow
Release: 2025-03-04 00:36:09
Original
240 people have browsed it

jQuery Dynamically Add Form Elements

Dynamically add form elements using jQuery/JavaScript. This is useful when the DOM is already loaded and you need to add additional input fields based on user action or selection (for example). You can create any new form element, not just input fields. Alternatively, you can create input elements in the page and simply hide them and display them again if needed.

Example: When changing password1 , insert a new input box named password1 after it is called password2.

// 监听 password1 字段的 change 事件以提示添加新输入框
$('#password1').change(function() {
    // 动态创建新的输入框并将其插入到 password1 之后
    $('<input type="password" id="password2" name="password2" placeholder="Confirm Password">').insertAfter('#password1');
});
Copy after login

The above is the detailed content of jQuery Dynamically Add Form Elements. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template