Button that loads a value coming from a classic ASP string to clipboard

Would it be possible to transport a value from a classic ASP string to the user his or her clipboard using pure javascript only?

I've found this example and tried to modify it to work with a hidden form input. But without succes.
LVL 1
SteynskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Yes.

From the url you posted
<p id="text_element">Copy this !</p>

<button onclick="copyToClipboard('text_element')">
    Copy to clipboard
</button>
<br/><br/><input type="text" placeholder="Paste here for checking" />

Open in new window

function copyToClipboard(elementId) {

  // Create an auxiliary hidden input
  var aux = document.createElement("input");

  // Get the text from the element passed into the input
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append the aux input to the body
  document.body.appendChild(aux);

  // Highlight the content
  aux.select();

  // Execute the copy command
  document.execCommand("copy");

  // Remove the input from the body
  document.body.removeChild(aux);

}

function log(){
	console.log('---')
}

Open in new window


Change the html to
<p id="text_element"><% response.write("Copy this !")%></p>

<button onclick="copyToClipboard('text_element')">
    Copy to clipboard
</button>
<br/><br/><input type="text" placeholder="Paste here for checking" />

Open in new window


Where <% response.write("Copy this !")%> can be your database or anything else that is called from the server.
0
SteynskAuthor Commented:
Thank you Scott for your very quick response. But would it be possible to hide the

<p id="text_element"><% response.write("Copy this !")%></p>

part? how should I do this? can I use<span> tags?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
What do you mean by hide?
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

SteynskAuthor Commented:
The value that should be copied to the clipboard should not be visible in the form.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
<button id="test">Click me</button>

Open in new window

$(function(){

   $('#test').on('click',function(){
     $.ajax({
        method: "POST",
        url: "process.asp",
        data: { name: "John" }
     })
  .done(function( data ) {
 
       copyToClipboard(data);

   });
 });
});
function copyToClipboard(elementId) {

  // Create an auxiliary hidden input
  var aux = document.createElement("input");

  // Get the text from the element passed into the input
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append the aux input to the body
  document.body.appendChild(aux);

  // Highlight the content
  aux.select();

  // Execute the copy command
  document.execCommand("copy");

  // Remove the input from the body
  document.body.removeChild(aux);

}

Open in new window


PROCESS.ASP
name = request.form("name")
response.write "hello "& name

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The above accepts a click, then  uses ajax to send data to an asp page that returns "hello John".  Then calls the js function to add to the clipboard.

I didn't test it, but I think that should work.
0
SteynskAuthor Commented:
Thank you very much Scott
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think that js is wrong

Use this instead

$(function(){

   $('#test').on('click',function(){
     $.ajax({
        method: "POST",
        url: "process.asp",
        data: { name: "John" }
     })
  .done(function( data ) {
 
       copyToClipboard(data);

   });
 });
});
function copyToClipboard(text) {

  // Create an auxiliary hidden input
  var aux = document.createElement("input");

  // Get the text from the element passed into the input
  aux.setAttribute("value", text);

  // Append the aux input to the body
  document.body.appendChild(aux);

  // Highlight the content
  aux.select();

  // Execute the copy command
  document.execCommand("copy");

  // Remove the input from the body
  document.body.removeChild(aux);

}

Open in new window

0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I changed this lines

  function copyToClipboard(elementId) {

  aux.setAttribute("value", document.getElementById(elementId).innerHTML);
 

to

function copyToClipboard(text) {

  aux.setAttribute("value", text);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.