Syntax error from copy to clipboard command = unable to copy Text and template

时间:2018-06-04 17:30:12

标签: javascript jquery html

Good day.

I've been trying top figure out why the copy to clipboard not working despite of putting the correct form name, please advise what would be the best command, i've been trying so hard couldn't seem to make it work.

here is the sample of the form or template im working on I was hoping to use the copy to clipboard function so i can copy the title and content of each line

if (window.event.srcElement == frm.copyform){
                frm.holdtext.value = Template;
                Copied=frm.holdtext.createTextRange();
                Copied.execCommand('copy');
                alert('Copied to Clipboard!');
            }

       
<script>
<script> }
				if (window.event.srcElement == frm.copyform){
					frm.holdtext.value = Template;
					Copied=frm.holdtext.createTextRange();
					Copied.execCommand('copy');
					alert('Copied to Clipboard!');
				}
			}
		</SCRIPT>
<head>
<body>
<!--Special comment-->
<!--Style-->
<style type="text/css"> 
/* Some Generic styles */
H1 {
    Color: #9c33ff;
    font-size: 20px;
    isplay: inline-block;
}

H2 {
    font-size: 15px;
    Color: #751aff;
    font-size: 13px;
}


Body {
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  width: 580px;
  height: 800px;
  border: none;
  font: normal 12px/1 "Trebuchet MS", Helvetica, sans-serif;
  color: rgba(2,2,2,1);
  text-align: center;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  background: -webkit-linear-gradient(-45deg, rgba(153,103,206,1) 0, rgba(247,221,200,0.57) 49%, rgba(140,226,226,1) 100%);
  background: -moz-linear-gradient(135deg, rgba(153,103,206,1) 0, rgba(247,221,200,0.57) 49%, rgba(140,226,226,1) 100%);
  background: linear-gradient(135deg, rgba(153,103,206,1) 0, rgba(247,221,200,0.57) 49%, rgba(140,226,226,1) 100%);
  background-position: 50% 50%;
  -webkit-background-origin: padding-box;
  background-origin: padding-box;
  -webkit-background-clip: border-box;
  background-clip: border-box;
  -webkit-background-size: auto auto;
  background-size: auto auto;
}
    label{
    display: inline-block;
    float: left;
    clear: left;
    width: 250px;
    text-align: Left;
}
input {
  display: inline-block;
  float: center;
}
TBODY {
    font-size: 14px;
    Color: #313133;
    font-size: 13px;
}    
    
#tickettemplate {
    font-size: 15px;
    Color: #313133;
    font-size: 13px;
}
</style>

<!--Html starting point-->

<h1>i</h1>
<h2>i</h2>

<P align=center><IMG 
src="" alt="" width="80" height="83"></P>
<FORM name=tickettemplate id=tickettemplate>

<!-- Table to contain page title and buttons -->
<TABLE>
  <TBODY>
  <TR>
    <TD align=center colSpan=4>
      <DIV></DIV></TD></TR>
  <TR>
    <TD><INPUT name=Reset onclick="resetForm();return false;" type=reset value="Clear All Fields"> 
    </TD>
    <TD><INPUT name=copyform onclick="val1();return false;" type=button value="Copy to Clipboard"> 
    </TD></TR></TBODY></TABLE><!-- Table for General information --><BR>

<!--This is where i started-->

<TABLE 
style="FONT-SIZE: 13pt; BORDER-TOP: gray solid; BORDER-RIGHT: gray solid; BORDER-BOTTOM: gray solid; BORDER-LEFT: gray solid" 
14pt? FONT-SIZE:><STRONG>General Info</STRONG></TD></TR> 
  <TBODY>
  <TR>
    <TD vAlign=middle>Name:</TD>
    <TD><INPUT name=cname_singleuser id=cname_singleuser size=50> 
</TD>
  <TR>
    <TD vAlign=middle>User ID:</TD>
    <TD><INPUT name=userid_singleuser0 id=userid_singleuser0 size=50> </TD></TR>
  <TR>
    <TD vAlign=top>Callback no.: </TD>
    <TD><INPUT name="callback no" id=callbackno0 size=50></TD></TR>
  <TR>
    <TD vAlign=middle>Email Address:</TD>
    <TD><INPUT name=emailaddy_singleuser id=emailaddy_singleuser 
size=50></TD></TR>
  <TR>
    <TD vAlign=middle>Office Location</TD>
    <TD><INPUT name=oloc_singleuser id=oloc_singleuser 
size=50></TD></TR>
  <TR>
    <TD vAlign=middle>Current Location:</TD>
    <TD><INPUT name=cloc_singleuser id=cloc_singleuser 
size=50></TD></TR>
  <TR>
   <TD vAlign=middle>Exisitng/Related:</TD>
    <TD><INPUT name=exrelated_singleuser id=exrelated_singleuser 
    size=50></TD></TR>
    <TD vAlign=top>Business Unit/Client:</TD>
    <TD><INPUT name=client1 id=client1 size=50> </TD></TR></TBODY></TABLE><BR>
<TABLE width=750 cellSpacing=0 cellPadding=0>
  <TBODY>

<TABLE 
style="FONT-SIZE: 13pt; BORDER-TOP: gray solid; BORDER-RIGHT: gray solid; BORDER-BOTTOM: gray solid; BORDER-LEFT: gray solid" 
14pt? FONT-SIZE:><STRONG>Brief Description</STRONG></TD></TR> 
  <TBODY>
  <TR>
    <TD vAlign=middle>Issue: </TD>
    <TD><INPUT name=Issue id=issue0 size=50></TD></TR>
  <TR>
    <TD vAlign=top>Number of Users Affected: </TD>
    <TD><INPUT name="Number of users Affected" id=numberofusers0 
size=50></TD></TR>
  <TR>
    <TD vAlign=top>Business Impact:</TD>
    <TD><SELECT name="Business Impact" id=businessimpact0 size=1 type="text"> 
        <OPTION value="" selected></OPTION> <OPTION value=Minor>P4 
        Minor</OPTION> <OPTION value=Medium>P3 Medium</OPTION> <OPTION 
        value=High>P2 High</OPTION> <OPTION value=Major>P1 
      Major</OPTION></SELECT> </TD></TR>
  <TR>
    <TD width=400 vAlign=top><p>Additional information/
      Incident Description:</p></TD>
    <TD><TEXTAREA name=Description id=Description1 rows=8 cols=60 wrap=virtual></TEXTAREA></TD></TR></TBODY></TABLE>
<TABLE width=750>
  <TBODY>
  <TR><BR><STRONG>Choose the Category:</STRONG></TR></TBODY></TABLE>

1 个答案:

答案 0 :(得分:0)

Not sure where you got that code from, but seems to be wrong. If you are copying the text from an input it is pretty straight forward. Focus, select, and copy from a click action.

function copy(event) {
  var selector = event.target.getAttribute('data-copy')
  var elem = document.querySelector(selector)
  elem.focus()
  elem.select()
  document.execCommand('copy')
}

document.querySelectorAll('[data-copy]').forEach( function () {
  this.addEventListener('click', copy)
})
<textarea id="foo">Hello world</textarea>
<button type="button" id="btnCopy1" data-copy="#foo">copy</button>
<hr/>
<textarea id="bar">Hello world</textarea>
<button type="button" id="btnCopy2" data-copy="#bar">copy</button>