Select and Copy Code from a Web Page Omitting Line Numbers
12:44 24 Jun 2026

I'm working on a "Programming Patterns" web page to provide college students with code snippets in the assemble language of a simulated computer. I've written JavaScript code to select the innerHTML of an element and copy it to the clipboard. However, I want to add line numbers to the displayed code so that accompanying text can refer to them, but not copy the line numbers. That is, students should see this:

 1.         LDA    label1  
 2.         ADD    label2  
 3.         OUT  
 4.         HLT     
 5. label1  DAT    15  
 6. label2  DAT    34  

But what should go to the clipboard should be this:

        LDA    label1  
        ADD    label2  
        OUT  
        HLT     
label1  DAT    15  
label2  DAT    34  

I have something working that involves putting each code snippet into a table with the line numbers in cells and code in calls. It's ugly and preparing and maintaining the code snippets is going to be a chore.

Is there a better way to do this?

javascript clipboard