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
- Validate form using jQuery
- How to pass javascript variable to laravel route on ajax call
- Uncaught TypeError: $.ajaxSetup is not a function
- Enable click event on appended element using jQuery
- Show loading icon on click submit button in jQuery
- How to remove Choose File button in jquery
- Uncaught TypeError: $.ajax is not a function
- How to preview image before uploading in jQuery
- How to reset form input element on click
- Jquery.validate.min.js:4 Uncaught TypeError: Cannot read properties of undefined (reading 'mode')
- How to validate input type file size in jQuery for multiple records
- How to get closest form input value in jQuery
- How to find closest upper element and append html to below it in jquery
- How to check object is empty or not in jQuery
- JQuery append html to below the target element
- Execute function after Ajax call is complete
- Preview image in BLOB URL format on choose file in jQuery
- How to create read more in jquery
- Ajax for multiple form
- How to check ajax request is completed in jQuery
- Append method to add text to body not working
- Jquery-3.6.0.min.js CDN link
- Ajax done fail ajaxcomplete in not a function
- Preview image in base64 format using jQuery on change input file
- (index):128 Uncaught TypeError: MobileNumberVal.test is not a function