Solved

JavaScript: to copy

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

632 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