Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the value of dynamically created textbox using jquery

Tags:

jquery

each

I'm having a hard time with getting the value of my dynamically appended textboxes. I'm using the $.each function to iterate all of the textboxes according to its id and index within the id.

<input type="text"  id="student_grde_G[1]" >
<input type="text"  id="student_grde_G[2]" >
<input type="text"  id="student_grde_G[3]" >

<input type="button" id="save_grade_button" class="button" value="Save Grades">

jQuery:

$('#save_grade_button').click(function (){
    $.each($('#student_grde_G[]'), function(i, item) {
        var grade =  $('#student_grde_G['+i+']').val();
        alert(grade);
    });
});

This doesn't work. Can anyone tell me how I can fix this?

like image 763
Aoi Avatar asked Jan 15 '23 13:01

Aoi


1 Answers

You can just use the item parameter:

$('#save_grade_button').click(function (){
     $('input[type=text]').each(function(i, item) {
         var grade =  $(item).val();
         alert(grade);
     });
 });

OR with @Adil's answer combined:

$('#save_grade_button').click(function (){
     $('[id^=student_grde_G]').each(function(i, item) {
         var grade =  $(item).val();
         alert(grade);
     });
 });
like image 195
Thijs Kramer Avatar answered Mar 23 '23 18:03

Thijs Kramer