Solved

JavaScript: to copy

Posted on 2013-02-04
3
313 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
Comment Utility
what if you change

holdtext.innerText = ...

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

Expert Comment

by:COBOLdinosaur
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

10 Experts available now in Live!

Get 1:1 Help Now