Solved

JavaScript: to copy

Posted on 2013-02-04
3
316 Views
Last Modified: 2013-02-04
Hello Experts,

I have the following code that works independantly, but when i implement it in a page with A LOT more controls and functions, i get "holdtext" is undefined.  Can any one see why, or suggest what to look for?

<html>

<body>


<table>
						
	</tr>
		<td colspan=2> 
			<b> Direct Links </b> <br> &nbsp;
		</td>
	</tr>
							
	<tr> 
		<td>
			#01: <input type="text" name="txtURLcsst" id="txtURLcsst" readonly=readonly size=75 value="https://www.abc.com?pc=One">
												 
			     <input type="button" name="btnCopyCSST" onClick="copyURL('CSST');" value="Copy to Clipboard">
		</td>
	</tr>
							
	<tr> <td> &nbsp; </td> </tr>
						
	<tr> 
		<td>
			#002: <input type="text" name="txtURLcssn" id="txtURLcssn" readonly=readonly size=75 value="Two">
												 
			      <input type="button" name="btnCopyCSSN" onClick="copyURL('CSSN');" value="Copy to Clipboard">
		</td>
	</tr>
							
	<tr> <td> &nbsp; </td> </tr>
							
	<tr> 
		<td>
			#003: <input type="text" name="txtURLsdtt" id="txtURLsdtt" readonly=readonly size=75 value="To Be Coded...">
												 
			      <input type="button" name="btnCopSDTT" onClick="copyURL('SDTT');" value="Copy to Clipboard">
		</td>
	</tr>
							
</table>
	<TEXTAREA ID="holdtext" name="holdtext" STYLE="display:block;"> </TEXTAREA>
						
</body>


<SCRIPT LANGUAGE="JavaScript">

function copyURL(textbox)
{
	if (textbox == 'CSST')
	{
		holdtext.innerText = txtURLcsst.value;
	}
	if (textbox == 'CSSN')
	{
		holdtext.innerText = txtURLcssn.value;
	}
	if (textbox == 'SDTT')
	{
		holdtext.innerText = txtURLsdtt.value;
	}
	
     Copied = holdtext.createTextRange();
     Copied.execCommand("Copy");
     alert('ok');
}


</SCRIPT>



</html> 

Open in new window


Thanks!
0
Comment
Question by:APD_Toronto
3 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 250 total points
ID: 38852094
what if you change

holdtext.innerText = ...

to document.getElementById('holdtext').innerText = ....
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38852110
I don't see anywhere that you create a handle to holtext or anything else so the script enging has absolutely no idea where to find it in the document object.

Generally post a frament of code just causes the process of finding a solution take longer.  If you post a link we can do diagnostics to get to a resolution more quickly.

By "works independently" I assume you used some tool.  Obviously it does not do a very goo job of emulating a real environment.  

Cd&
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 250 total points
ID: 38852159
Hi
I found your HTML code is very bad. (not having closing tags)

Check this example. This is working for me. (Tested in Chrome and IE)

<table>						
	<tr>
		<td> 
            <b> Direct Links </b> <br /> &nbsp;
		</td>
	</tr>							
	<tr> 
		<td>
			#01: <input type="text" name="txtURLcsst" id="txtURLcsst" readonly="readonly" size="75" value="https://www.abc.com?pc=One" />											 
			     <input type="button" name="btnCopyCSST" onclick="copyURL('CSST');" value="Copy to Clipboard" />
		</td>
	</tr>							
	<tr> <td> &nbsp; </td> </tr>						
	<tr> 
		<td>
			#002: <input type="text" name="txtURLcssn" id="txtURLcssn" readonly="readonly" size="75" value="Two" />											 
			      <input type="button" name="btnCopyCSSN" onclick="copyURL('CSSN');" value="Copy to Clipboard" />
		</td>
	</tr>							
	<tr> <td> &nbsp; </td> </tr>							
	<tr> 
		<td>#003: 
            <input type="text" name="txtURLsdtt" id="txtURLsdtt" readonly="readonly" size="75" value="To Be Coded..." />												 
			<input type="button" name="btnCopSDTT" onclick="copyURL('SDTT');" value="Copy to Clipboard" />
		</td>
	</tr>							
</table>
	<textarea id="holdtext" rows="4" cols="38" name="holdtext" style="display:block;"> </textarea>
	
  <script type="text/javascript">
        function copyURL(txt) {
            alert(txt);
            debugger;
            var holdtextObject = document.getElementById('holdtext');
            if (txt == 'CSST') {
                holdtextObject.innerText = document.getElementById('txtURLcsst').value;
            }
            if (txt == 'CSSN') {
                holdtextObject.innerText = document.getElementById('txtURLcssn').value;
            }
            if (txt == 'SDTT') {
                holdtextObject.innerText = document.getElementById('txtURLsdtt').value;
            }

            Copied = holdtextObject.createTextRange();
            Copied.execCommand("Copy");
            alert('ok');
        }
    </script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now