简体   繁体   中英

How to Grab input values of dynamically added input boxes using jquery

I am trying to add dynamic fields on click and want to fetch each input box value that user types. Here is my working code. HTML code

<div class="input_fields_wrap">
 <div><input type="text" name="mytext"> <button class="add_field_button">Add More Fields</button>
</div>
</div>

JS code

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
   var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><tr><td><input type="text" name="textbox' + x + '" id="removeId' + x + '" value="' + x + '" ></td><td><button class="remove_field" >Remove</button><button class="save_field" >save</button></td></tr></div>'); //add input box

        }
    });
 $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); 
        $(this).parent('div').remove(); 
        x--;
    })
    $(wrapper).on("click",".save_field", function(e){ //user click on remove text
        e.preventDefault(); 
        //Here on click of each save button I wanna grab particular text box value that user types in
})
});

On each click of add more fields I get new text box along with remove and save button, upon clicking on save button I want to grab its respective text box value that user has typed.

JSFiddle

PFB code , hope it helps

 $(document).ready(function() { var max_fields = 10; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID var x = 1; //initlal text box count $(add_button).click(function(e) { //on add input button click e.preventDefault(); if (x < max_fields) { //max input box allowed x++; //text box increment $(wrapper).append('<div><tr><td><input type="text" name="textbox' + x + '" id="removeId' + x + '" value="' + x + '" ></td><td><button class="remove_field" >Remove</button><button class="save_field" >save</button></td></tr></div>'); //add input box } }); $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text e.preventDefault(); $(this).parent('div').remove(); x--; }) $(wrapper).on("click", ".save_field", function(e) { //user click on remove text var inpVal = $(this).siblings('input').val(); alert('inp value is :' + inpVal); e.preventDefault(); //Here on click of each save button I wanna grab particular text box value that user types in }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="input_fields_wrap"> <div> <input type="text" name="mytext"> <button class="add_field_button">Add More Fields</button> </div> </div> 

clicked button is sibling of required input tag. you can use .siblings('input') or .prevAll('input') to target input element along with .val() to get its value:

$(this).siblings('input').val();

Handler:

$(wrapper).on("click",".save_field", function(e){ //user click on remove text
    e.preventDefault(); 
    alert($(this).siblings('input').val());
    //Here on click of each save button I wanna grab particular text box value that user types in
});

Working Demo

you can give dynamic id to your textboxes replace:

id="removeId'

with

id="removeId'+k

And then you can access them using

$("#id").val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM