0

I have a JSP page that looks like this:

enter image description here

I added a button Copy URL and it's self explanatory that when I click the button, I need to copy the unique URL for that particular link itself.

This is how my front end looks:

<%
    //need to input logic to populate data on each row

    int counter=0;

    String[] split = request.getParameter("nodeID").split(",",0);
    for(int i=0;i<split.length;i++){

        long file=Long.parseLong(split[i]);

        List files = fileFacade.list_items(file);
        for (Iterator rstltr = files.iterator(); rstltr.hasNext();) {
            Fmedia fv = (Fmedia) rstltr.next();
            Node nd = nodeFacade.get(fv.getNodeid(), false);
            // Fmedia fm = fileFacade.get_file(fv.getNodeid());

            int count = 0;
            count++;
            long fileid= nd.getNodeid();



%>


    <tbody>
        <tr>
            <td width="5%">
                <!--Display Checkbox   -->
                <input type="checkbox" name="name1" />&nbsp;
            </td>

            <td>
                <!--Display No   -->
                <% counter=counter+1;
                    out.print(counter);

                %>


            </td>
            <td width="28%">

                <!-- Display Filename   -->
                <%=nd.getNodedesc()%>



            </td>
            <td width="100%">
                <!-- Display URL -->


                <%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>
                <%

                    fileFacade.insert_url(nd.getNodeid(),"http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid());


                %>


            </td>
            <td>
                <!-- Display EDIT/DEL -->

            </td>
            <td> <!-- Display COPY feature -->

                <input type="button" value="Copy URL" onclick="msg()">
                <script>
                    function msg() {
                        alert("Hello world!");
                    }
                </script>
            </td>


        </tr>

        </tbody>


    <%}}
    %>

I need a way that each button is independent of each row and a javascript function to be able to copy the link after selecting the checkbox.

I am not too sure how to proceed from here.

Can anyone suggest a good approach?

EDIT:

Javascript function:

<input type="button" value="Copy URL" onclick="getURL()">
            <script>
                function getURL() {
                    var copyText = "http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+<%nd.getNodeid();%>
                    var el = document.createElement('textarea');
                    el.value = copyText;
                    el.setAttribute('readonly', '');
                    el.style = {
                        position: 'absolute',
                        left: '-9999px'
                    };
                    document.body.appendChild(el);
                    el.select();
                    document.execCommand('copy');
                    document.body.removeChild(el);
                }
            </script>
        </td>
Daredevil
  • 1,672
  • 3
  • 18
  • 47

1 Answers1

1

You have to pass URL to the function, follow the below code

<tbody>
    <tr>
        <td width="5%">
            <!--Display Checkbox   -->
            <input type="checkbox" name="name1" />&nbsp;
        </td>

        <td>
            <!--Display No   -->
            <% counter=counter+1;
                out.print(counter);

            %>


        </td>
        <td width="28%">

            <!-- Display Filename   -->
            <%=nd.getNodedesc()%>



        </td>
        <td width="100%">
            <!-- Display URL -->


            <%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>
            <%

                fileFacade.insert_url(nd.getNodeid(),"http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid());


            %>


        </td>
        <td>
            <!-- Display EDIT/DEL -->

        </td>
        <td> <!-- Display COPY feature -->

            <input type="button" value="Copy URL" onclick="copyURL('<%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>')">

        </td>


    </tr>

    </tbody>


<%}}
%>

            <script>
                function copyURL(url) {
                    var copyText = url;
                var el = document.createElement('textarea');
                el.value = copyText;
                el.setAttribute('readonly', '');
                el.style = {
                    position: 'absolute',
                    left: '-9999px'
                };
                document.body.appendChild(el);
                el.select();
                document.execCommand('copy');
                document.body.removeChild(el);
                }
            </script>
Googlian
  • 6,077
  • 3
  • 38
  • 44