Solved

JavaScript: to copy

Posted on 2013-02-04
3
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 33

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

734 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