I'm trying to copy a row in html, I have a table contains 4 columns AccName. AccCurrName. AccRaseed. CopyButton.
I'm working with Laravel and I'm using the following code:
@extends('layouts.master')
@section('css')
<link href="{{ URL::asset('assets/plugins/datatable/css/dataTables.bootstrap4.min.css') }}" rel="stylesheet" />
<link href="{{ URL::asset('assets/plugins/datatable/css/buttons.bootstrap4.min.css') }}" rel="stylesheet">
<link href="{{ URL::asset('assets/plugins/datatable/css/responsive.bootstrap4.min.css') }}" rel="stylesheet" />
<link href="{{ URL::asset('assets/plugins/datatable/css/jquery.dataTables.min.css') }}" rel="stylesheet">
<link href="{{ URL::asset('assets/plugins/datatable/css/responsive.dataTables.min.css') }}" rel="stylesheet">
<link href="{{ URL::asset('assets/css/joul.css') }}" rel="stylesheet">
<link href="{{ URL::asset('assets/css/iziToast.min.css') }}" rel="stylesheet">
@endsection
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th style="width: 50%;">Cus</th>
<th style="width: 20%;">Curr</th>
<th style="width: 20%;">Raseed</th>
<th style="width: 10%;">Copy</th>
</tr>
</thead>
<tbody>
@foreach($result as $x)
<tr>
<td class="copy-text">{{$x->AccName}}</td>
<td class="copy-text">{{$x->AccCurrName}}</td>
<td class="copy-text">{{$x->AccRaseed}}</td>
<td>
<a class="modal-effect btn btn-sm btn-info copy-button"
data-effect="effect-scale"
data-toggle="modal"
data-id="{{ $x->AccName }}"
data-clipboard-target=".copy-text">
<i class="las la-pen"></i> Copy
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
@section('js')
<!-- Internal Data tables -->
<script src="{{ URL::asset('assets/plugins/datatable/js/jquery.dataTables.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/dataTables.dataTables.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/dataTables.responsive.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/responsive.dataTables.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/dataTables.bootstrap4.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/dataTables.buttons.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/buttons.bootstrap4.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/jszip.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/pdfmake.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/vfs_fonts.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/buttons.html5.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/buttons.print.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/buttons.colVis.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/dataTables.responsive.min.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/datatable/js/responsive.bootstrap4.min.js') }}"></script>
<script src="{{ URL::asset('assets/js/table-data.js') }}"></script>
<script src="{{ URL::asset('assets/js/modal.js') }}"></script>
<script src="{{ URL::asset('assets/plugins/jquery.maskedinput/jquery.maskedinput.js') }}"></script>
<script src="{{ URL::asset('assets/js/form-elements.js') }}"></script>
<script src="{{ URL::asset('assets/js/joul.js') }}"></script>
<script src="{{ URL::asset('assets/js/iziToast.min.js') }}"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const copyButtons = document.querySelectorAll('.copy-button');
copyButtons.forEach(button => {
button.addEventListener('click', function() {
const rowId = this.getAttribute('data-clipboard-target');
const rowContent = document.querySelector(rowId).innerText;
const tempTextarea = document.createElement('textarea');
tempTextarea.value = rowContent;
document.body.appendChild(tempTextarea);
tempTextarea.select();
document.execCommand('copy');
document.body.removeChild(tempTextarea);
iziToast.success({
title: 'Done',
message: 'Copied!',
position: 'bottomRight'
});
});
});
});
</script>
@endsection
but that code copies the first column from the first rows, what is the problem in? I tried to apply many solution but without result. please help me to know the problem