1

I have so many Input fields in html table and I am using the code below to copy paste columns from excel directly to these input fields. and the code below is working great for copy paste columns from excel to this html input table fields. My problem is that I want onchange event for each input to detect when I copy paste and alert these values. cause then I can take each value was changed and change it in the database. The Onchange event does not detect change in value when I copy paste!

<table>
<thead>

<tr>
      <th>Name</th>
      <th>Unit</th>
      <th >ID</th>
      <th >Margin </th>
</tr>


<tbody>
<?php
 $query = " select  name,unit,Margin, id FROM Table1";

                             $stmt = $conn->query( $query ); 
                             $count=0;
                              while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) )

 { ?>


<tr>
<td> <input onchange="send(this)" type="text" value="<?php echo $row['Name']; ?>">   </td>
<td><input onchange="send(this)" type="number" value="<?php echo $row['unit]; ?>" ></td>
<td><input  type="number" value="<?php echo $row['ID']; ?>" ></td>
<td><input onchange="send(this)" type="number" value="<?php echo $row['Margin']; ?>" ></td>

</tr>

<?php } ?>

</tbody>
</thead>
</table>


    <!-- Copy Paste in input fields -->
   <script type="text/javascript">
$('input').on('paste', function(e){
    var $this = $(this);
    $.each(e.originalEvent.clipboardData.items, function(i, v){
        if (v.type === 'text/plain'){
            v.getAsString(function(text){
                var x = $this.closest('td').index(),
                    y = $this.closest('tr').index()+1,
                    obj = {};
                text = text.trim('\r\n');
                $.each(text.split('\r\n'), function(i2, v2){
                    $.each(v2.split('\t'), function(i3, v3){
                        var row = y+i2, col = x+i3;
                        obj['cell-'+row+'-'+col] = v3;
                        $this.closest('table').find('tr:eq('+row+') td:eq('+col+') input').val(v3);
                    });
                });
            });
        }
    });
    return false;
   
});
   </script>
<script>

function send(value){

alert(value.value);

}

</script
  • 1
    The `change` event fires when you leave an input after modifying it. Use `input` and `edit` to detect when the change happens immediately. – Barmar Aug 05 '22 at 20:35
  • @Barmar excuse me I just did not get your point " Use input and edit ", can you explain to me pls – Mahmoud Hassan Aug 05 '22 at 20:40
  • Use `oninput` instead of `onchange`. – Barmar Aug 05 '22 at 20:41
  • 1
    @Barmar No sir it did not work too ... oninput works when I change the input manually but also does not work when I copy paste! – Mahmoud Hassan Aug 05 '22 at 20:49
  • Please [edit] your question to include a [mre] showing the problem. As it is, it shows invalid HTML (because of the PHP code layered in). Also, you have script that handles the `paste` event and returns false, which stops any other event handlers from receiving the event. Why don't you just use `$(this).trigger('change')` in your `paste` event handler? – Heretic Monkey Aug 05 '22 at 21:04
  • There is an `onpaste` event you could use. It is technically experimental but most browsers already support it. Just be aware that at the time of the event firing the contents of the input have yet to be updated. Because of this, if you want the value you will need to use a setTimeout to wait a few ms for the content to get updated. See here for more details: https://stackoverflow.com/questions/3211505/detect-pasted-text-with-ctrlv-or-right-click-paste – Daniel Black Aug 05 '22 at 21:07

1 Answers1

0

I use this for my input for cut ,copy & paste

function send(value){

alert(value.value);

}
input here  :     <input onchange="send(this)" type="text"  oncopy="send(this)" onpaste="send(this)" oncut="send(this)" type="text" value=""/>
Sund'er
  • 666
  • 1
  • 4
  • 11