0

I'm trying to copy all of the content from a p tag using javascript. It works, but it copies the text in one long line of text. I want it to copy with the format using the break tags, like if someone were to copy / paste using their mouse like so for an example:

Current Time: :

Time Found: :

Ambient Temp: °C

Notes:

Frequency:

Reproducible?

Here is my component looks like:

<div class="slds-box slds-theme_shade slds-theme_alert-texture ">                                           

<span style="margin-left: 15px; float: right"  >
     <lightning:button onclick="{!c.onCopy}" label="Copy"                                                                                
         value="<h2>Documentation Sheet</h2><br>Current Time: :<br>
 Time Found: :<br>Ambient Temp: °C<br><br><br>Notes:<br><br>Frequency:<br>Reproducible? </p>" aura:id="btn1"/>
</span>

</div>

my controller JS:

onCopy: function(component, event, helper) {
    helper.copy(component, event, event.getSource().get("v.value"));
}, 

my helper controller JS:

copy: function(component,event,text) {
        // Create an hidden input
        var hiddenInput = document.createElement("input");
        // passed text into the input
        hiddenInput.setAttribute("value", text);
        // Append the hiddenInput input to the body
        document.body.appendChild(hiddenInput);
        // select the content
        hiddenInput.select();
        // Execute the copy command
        document.execCommand("copy");
        // Remove the input from the body after copy text
        document.body.removeChild(hiddenInput);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.