Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to copy my HTML table elements to the clipboard. But I have used div tag inside the td tag of the table. I think that what adding a new line after pasting the contents from clipboard. I want that to stay in the same line. Please help how to make it possible.

What I have tried:

Sample Output

ProfileUID : 12345
First Name : John
Email : John.doe@xyz.com
Profile Status : Enabled
Country Code : United States (+1)
Phone No. :
+1- 456789125

Last Generated OTP : x5xx555
Employee ID : 223344
User ID : johndoe

Required Output

ProfileUID : 12345
First Name : John
Email : John.doe@xyz.com
Profile Status : Enabled
Country Code : United States (+1)
Phone No. : +1- 456789125
Last Generated OTP : x5xx555
Employee ID : 223344
User ID : johndoe

Have a look at the Phone No. texts in both the above outputs.

index.html

<pre lang="HTML">

<div style="margin-left: 2rem">
	<button class="btn btn-primary" type="button" style="background-color: #001872; display: none" onclick="copyTable(document.getElementById('table'))" id="copy"> class="fa-regular fa-copy" id="table" align="center" name=""> </button>
							<button type="submit" id="save" style="display:none; border-style: none; background-color:transparent; color:#17d647e8; padding: 2px;" onclick="saved()">^__i class="fa-solid fa-square-check fa-2x"></button>
							<button type="button" id="cancel" style="display:none; border-style: none; background-color:transparent; color:#c42c2c;">^__i class="fa-solid fa-square-xmark fa-2x" onclick="canceled()"></button>
						</div>
				</td>
			</form>
		</tr>
		<tr align="center">
			<th style="width: 20%" class="col-sm-4 text-center" scope="row"> Last Generated OTP : </th>
			<td style="width: 50%"></td>
		</tr>
		<tr align="center">
			<th style="width: 20%" class="col-sm-4 text-center" scope="row">Employee ID :</th>
			<td style="width: 50%"></td>
		</tr>
		<tr align="center">
			<th style="width: 20%" class="col-sm-4 text-center" scope="row">User ID :</th>
			<td style="width: 50%"></td>
		</tr>
	</tbody>
</table>


script.js

JavaScript
function copyTable(el) {
  var body = document.body,
    range,
    sel;
  if (document.createRange && window.getSelection) {
    range = document.createRange();
    sel = window.getSelection();
    sel.removeAllRanges();
    try {
      range.selectNodeContents(el);
      sel.addRange(range);
    } catch (e) {
      range.selectNode(el);
      sel.addRange(range);
    }
  } else if (body.createTextRange) {
    range = body.createTextRange();
    range.moveToElementText(el);
    range.select();
  }
  document.execCommand("Copy");
}
Posted
Comments
Member 15627495 22-Jun-22 0:08am    
both your Html and Js go too often with naming convention errors :
you use keywords ( protected ) as names for vars, let's see :
id="table"
var body / range

close your functions calls by ";" => onclick="the_function();" it will be usefull when you have more than one function to call by onclick for example.

to help you to use clipboard : read that page
https://www.w3schools.com/howto/howto_js_copy_clipboard.asp
Member 4590579 17-Nov-22 5:58am    
is el a table name? The format of HTML in index.html is not correct. Missing table tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900