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
Solved

JavaScript: to copy

Posted on 2013-02-04
3
319 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

840 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