
Add and remove input fields on click button using jQuery
Add and remove input fields on click button using jQuery
Through the use of the programming language, we will work together to solve the "Add and remove input fields on click button using jQuery" puzzle in this lesson.
You can add and remove input fields dynamically on click button using jQuery. JQuery helps to append and remove input fields to a HTML element in an HTML document.-
Add and remove input fields on click button using jQuery
<!-- HTML --> <div id="websiteUrlWrapper"> <div class="form-group mb-0"> <label for="websiteUrl">Website URL</label> </div> <div class="form-row form-group"> <div class="col-md-5"> <input type="text" class="form-control" id="websiteUrl" name="website_url[]" value="w3codegenerator.com"> </div> <div class="col-md-5"> <select class="form-control" id="websiteUrlType" name="website_type[]"> <option value="1"> Personal </option> <option selected="" value="2"> Company </option> <option value="3"> Blog </option> <option value="4"> RSS feed </option> <option value="5"> Portfolio </option> <option value="6"> Other </option> </select> </div> <div class="col-md-2"> <button type="button" name="remove" class="btn btn-danger remove"><i class="fa fa-remove"></i></button> </div> </div> </div> <button type="button" name="add" class="btn btn-success btn-sm add mb-3"><i class="fa fa-plus" aria-hidden="true"></i> Add website </button> <p>JQuery script </p> <script> $(document).on('click', '.add', function(){ var html = '<div class="form-row form-group">'; html +='<div class="col-md-5">'; html += '<input type="text" class="form-control" id="websiteUrl" name="website_url[]" value="">'; html += '</div>'; html +='<div class="col-md-5">'; html += '<select class="form-control" id="websiteUrlType" name="website_type[]">'; html += '<option selected="" value="1"> Personal </option>'; html += '<option value="2"> Company </option>'; html += '<option value="3"> Blog </option>'; html += '<option value="4"> RSS feed </option>'; html += '<option value="5"> Portfolio </option>'; html += '<option value="6"> Other </option>'; html += '</select>'; html += '</div>'; html += '<div class="col-md-2">' html += '<button type="button" name="remove" class="btn btn-danger remove"><i class="fa fa-remove"></i></button>'; html += '</div>'; html += '</div>'; $('#websiteUrlWrapper').append(html); }); $(document).on('click', '.remove', function(){ $(this).closest('.form-group').remove(); }); </script>
This code snippet helps you to add input fields dynamically using jQuery. This code snippet helps you to add multiple input fields using the append method and remove input fields using jQuery remove method.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Jquery
- Enable click event on appended element using jQuery
- How to validate input type file size in jQuery for multiple records
- Preview image in base64 format using jQuery on change input file
- How to check object is empty or not in jQuery
- Execute function after Ajax call is complete
- JQuery find text and show as bold in html page
- Mixed Content: The page at 'https://w3codegenerator.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
- Ajax for multiple form
- Uncaught TypeError: $.ajaxSetup is not a function
- Jquery-3.6.0.min.js CDN link
- Jquery.validate.min.js:4 Uncaught TypeError: Cannot read properties of undefined (reading 'mode')
- (index):128 Uncaught TypeError: MobileNumberVal.test is not a function
- Uncaught TypeError: $.ajax is not a function
- How to create read more in jquery
- How to preview image before uploading in jQuery
- How to check ajax request is completed in jQuery
- How to find closest upper element and append html to below it in jquery
- Append method to add text to body not working
- How to reset form input element on click
- Show loading icon on click submit button in jQuery
- Get the href link on click anchor in javascript
- How to use and purpose of localstorage in javascript
- Preview image in BLOB URL format on choose file in jQuery
- How to get closest form input value in jQuery
- JQuery append html to below the target element