Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jayanthkumar's avatar

Hi How to push array value to text field

hi i am using check box to get the value of that table...i need to send that array based on check box so here is my script...i am able to get that array in variable "array_id" but not able put the same in text box

<p1 type="text" id="textfield"></p1>
<script>  
 $(document).ready(function(){  
      $('#btn-mail').click(function(){  
           if(confirm("Are you sure you want to Proceed?"))  
           {  
                var array_id = [];  

                $(':checkbox:checked').each(function(i){  
                     array_id[i] = $(this).val(); 
                     array_id.push($(this).find(('#textfield').val());
                     
                });  
                console.log(array_id);
                 
                
                // if(id.length === 0) //tell you if the array is empty  
                // {  
                //      alert("Please Select atleast one checkbox");  
                // }  
                // else  
                // {  
                //      $.ajax({  
                //           url:'sendmail',  
                //           method:'POST',  
                //           data:{id:id},  
                //           success:function()  
                //           {  
                //                for(var i=0; i<id.length; i++)  
                //                {  
                //                     $('tr#'+id[i]+'').css('background-color', '#ccc');  
                //                     $('tr#'+id[i]+'').fadeOut('slow');  
                //                }  
                //           }  
                //      });  
                // }  
           }  
           else  
           {  
                return false;  
           }  
      });  
 });  
 </script>  
0 likes
8 replies
DmytroOlefyrenko's avatar

You need to do that way to access text field data:

array_id.push($('#textfield').val());

Because this refers to looped checkbox. And do you need to add this value in the loop each time?

Jayanthkumar's avatar

Hi @DmytroOlefyrenko i have changing the script as u mentioned but still i am not able to get that value in textfield

<script>  
 $(document).ready(function(){  
      $('#btn-mail').click(function(){  
           if(confirm("Are you sure you want to Proceed?"))  
           {  
                var array_id = [];  

                $(':checkbox:checked').each(function(i){  
                     array_id[i] = $(this).val(); 
                     array_id.push($('#textfield').val());
                     
                     
                });  
                console.log(array_id);
                
                 
                
                // if(id.length === 0) //tell you if the array is empty  
                // {  
                //      alert("Please Select atleast one checkbox");  
                // }  
                // else  
                // {  
                //      $.ajax({  
                //           url:'sendmail',  
                //           method:'POST',  
                //           data:{id:id},  
                //           success:function()  
                //           {  
                //                for(var i=0; i<id.length; i++)  
                //                {  
                //                     $('tr#'+id[i]+'').css('background-color', '#ccc');  
                //                     $('tr#'+id[i]+'').fadeOut('slow');  
                //                }  
                //           }  
                //      });  
                // }  
           }  
           else  
           {  
                return false;  
           }  
      });  
 });  
 </script>  
DmytroOlefyrenko's avatar

Hm... Try to change

<p1 type="text" id="textfield"></p1>

To

<input type="text" id="textfield" />

Also what do you see in console after that change?

console.log($('#textfield').val());
ejdelmonico's avatar

It seems like you are trying to push the array into an input field. Have you tried changing those lines to

$(':checkbox:checked').each(function(i){  
      array_id[i].push($('#textfield').val());
});
Jayanthkumar's avatar

hi @ejdelmonico this is also not working

var array_id = [];  
                     $(':checkbox:checked').each(function(i){  
                     array_id[i].push($('#textfield').val());
                     });

this error i am getting in console when i run code

Uncaught TypeError: array_id[i].push is not a function(…)

Please or to participate in this conversation.