JQuery Selectors – How can I copy to clipboard with different ids

            <h3>Something here</h3>
            <p id="copythis">Copy this code</p>

            <h3>Something here</h3>
            <p id="copythisone">Copy this other text</p>

            <h3>Something here</h3>
            <p id="copythisone">Copy this other text</p>


    <script type="text/javascript">
        $(document).ready(function(){
            $('#copythis').click(function(){

                var text = $("#copythis").get(0)
                var selection = window.getSelection();
                var range = document.createRange();
                range.selectNodeContents(text);
                selection.removeAllRanges();
                selection.addRange(range);
                document.execCommand('copy');
            })
        });
    </script>

I have different texts to copy (not all at once).

this code works for one text only. How do I work for more than one?

I just changed this part and it worked:

```var text = $(this).get(0)```

Thaks to @wahwahwah

Answer

$('#copythis').on("click", function(){
    console.log($(this).text() + ": you clicked on '#copythis' ");
});

$('.copy').on("click", function(){
    console.log($(this).text() + ": you clicked on a element with the class 'copy'");
});

$('p').on("click", function(){
    console.log($(this).text() + ": you clicked on a <p> element'");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Something here</h3>
<p id="copythis" class="copy">Copy this code</p>

<h3>Something here</h3>
<p id="copythisone" class="copy">Copy this other text</p>

<h3>Something here</h3>
<p id="copythisone" class="copy">Copy this other text</p>

With JQuery, you can use .text() to get the contents of a p element. You could also change your selector to just grab the contents of all ‘p’ elements. The ID selector .(#copythis) will grab the element related to only that ID. The class selector (.copy) will attach to all elements with the class “copy.”

This will help you isolate what’s being clicked on. What you want to do with it – copy the contents to clipboard – might change the logic a bit depending on if you have control over the HTML source, and how your deciding what gets copied and doesn’t.