Assistance required: Click on the button to select and copy all text within the specified paragraph ID

Hey there! I've got a div with a dropdown menu that reveals text and images based on the selected option. What I want to achieve is having a button that allows users to copy all the content inside the div.

Below is my sample code for the div dropdown:

<div class="button dropdown"> 
<select id="colorselector">
<option>Select Email templates here</option>
<option value="1st">R17 Update</option>
<option value="2nd">Reset Password</option>
</select>
</div>

Upon selection from the dropdown, it displays relevant text and images within the div.

<!---------------------------- P ID 1st---------------------->
<div id="1st" class="colors yellow"> 
<br> 
<p>Sample text</p>
<br>
<p>Sample text/p> 
<br>
<p>Sample text</p>
<br>
<p>Sample Image</a></p>
<img src="https://i.screenshot.net/dl/8j82ein?name=4.jpg" alt="">
<br>
<br> 
</div>

<!---------------------------- P ID 2nd---------------------->
<div id="2nd" class="colors red">
<br> 
<p>Sample text</p>
<br>
<p>Sample text/p> 
<br>
<p>Sample text</p>
<p>Sample Image</p>
<img src="https://i.screenshot.net/dl/8j82ein?name=4.jpg" alt="">
<br>
<font color="#08298A">Kind Regards,
<br> 
</div>

Now, I'm looking to add a button that will help in copying the highlighted text/image within the specific P ID.

For instance:

A user selects the "R17 Update" option from the dropdown

The related content displayed would be:

Sample text

Sample text

Sample text

Sample Image (Image here)

When the user clicks the following button, it should highlight or select all content of the P I.D for copying.

Thank you in advance for any assistance!

Answer №1

This script enables the hiding of templates by default and reveals them upon the button click event. To implement this functionality, ensure that all your templates have the template class.

function showTemplate() {
  // hide all templates
  var templates = document.getElementsByClassName('template');
  for(var i = 0; i < templates.length; i++) {
    templates.item(i).classList.remove('show');
  }
  
  // display selected template
  selector = document.getElementById('colorselector');
  templateId = selector.value;
  if (templateId !== '') {
    template = document.getElementById(templateId);
    template.classList.add('show');
  }
}
.template {
  display: none;
}

.template.show {
  display: block;
}
<div class="button dropdown"> 
<select id="colorselector">
<option value="">Select Email templates here</option>
<option value="1st">R17 Update</option>
<option value="2nd">Reset Password</option>
</select>
</div>

<button onclick="showTemplate()">Show template</button>

<!---------------------------- P ID 1st---------------------->
<div id="1st" class="colors yellow template"> 
<br> 
<p>Sample text</p>
<br>
<p>Sample text/p> 
<br>
<p>Sample text</p>
<br>
<p>Sample Image</a></p>
<img src="https://i.screenshot.net/dl/8j82ein?name=4.jpg" alt="">
<br>
<br> 
</div>

<!---------------------------- P ID 2nd---------------------->
<div id="2nd" class="colors red template">
<br> 
<p>Sample text</p>
<br>
<p>Sample text</p> 
<br>
<p>Sample text</p>
<p>Sample Image</p>
<img src="https://i.screenshot.net/dl/8j82ein?name=4.jpg" alt="">
<br>
<font color="#08298A">Kind Regards,
<br> 
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Scene three js appears to be stuck in a perpetual state of emptiness, making it challenging to start

I've encountered a major issue with understanding how to use three.js. Despite my best efforts over the past few days, I haven't been able to successfully implement three.js into my projects. Initially, I attempted using Parcel by starting a new ...

Creating a table in VueJs and populating it with values retrieved from an MSSQL database in a .NET Core web application

I am developing a table within a .NET Core Web Application that includes multiple rows and columns filled with data retrieved from a MSSQL server through a WEB API Given the need for numerous rows and columns, I am considering using a for loop inside my & ...

The HTML to PDF file converter API performs well in a local environment but encounters issues when deployed on node.Js, Express.Js, html-pdf, and Azure Web Services platform

I've developed an API that converts HTML to PDF, and it works flawlessly in my local environment but encounters issues when deployed on Azure app web services. During the process of generating the PDF, the request gets stuck and eventually times out, ...

In JavaFX, when adjusting the transparency of a popup window, only the brightness changes while the solid color remains unchanged, effectively concealing

Is it possible to have a transparent popup window (errorDialog.fxml) on top of the main window (mainWindow.fxml) with 50% opacity while still showing the content of the main window underneath? My attempts at setting the background color of the overlay insi ...

A step-by-step guide on extracting all documents within a directory collection from the official national archives website using the R programming language

I'm currently seeking a way to scrape all available files for a data file series on archive.gov programmatically using R. It seems that archives.gov utilizes JavaScript. My objective is to extract the URL of each available file along with its name. T ...

interactive symbols in a reorganizable tree structure

I have been developing a customizable tree list for our users to arrange their website content. It allows them to add and rearrange pages by dragging and dropping. Each list item includes the page name and three icons (lock, visible, and edit). The challe ...

Encountering a NaN outcome when summing values from various select options

I'm working on a project that involves adding up the prices based on the surface chosen by the user. I'm struggling with calculating the partial cost when the user's choice changes. totalSum.ts num: Calculation totalAmount: number cate ...

What is the method to convert Javascript values from pixels to percentages?

Is it possible to change the scrolltop value dynamically based on a percentage of the user's screen size? I've been trying to achieve this using JS but haven't had any luck. Here is a link to a codepen that showcases the issue: [link] (http ...

Unable to view videos shared by users in PeerJS and WebRTC video chat application from different tabs

Recently, I embarked on the task of creating a Video chat Website using Peer Js. Initially, everything seemed to be working fine as I was able to see my own video stream. However, a problem arose when attempting to view the video stream from another tab or ...

Is it possible to incorporate custom scripts into the <head> section of the index.html file in Docusaurus?

I decided to organize my code by creating a scripts folder within the static directory. Inside this folder, I added a custom JavaScript file named "GetLocation.js". The path to this file is project/website/static/scripts/GetLocation.js Upon looking into s ...

The Angular custom modal service is malfunctioning as the component is not getting the necessary updates

As I develop a service in Angular to display components inside a modal, I have encountered an issue. After injecting the component content into the modal and adding it to the page's HTML, the functionality within the component seems to be affected. F ...

The overflow-anchor property is not effective for scrolling horizontally

My goal is to create a never-ending horizontal scroll that allows users to scroll both left and right. When users scroll to the left, new content should be added to the beginning of the scrollable element (similar to scrolling through a schedule history). ...

The TypeScript `unknown` type restricts the use of non-unknown types in function parameters

Why is there an error in this code? const x: unknown[] = ['x', 32, true]; // OK const y: (...args: unknown[]) => unknown = (xx: number) => {}; // ERROR // Type '(xx: number) => void' is not assignable to type '(...args: u ...

Ways to retrieve the innerHTML of a Script Tag

After spending what feels like an eternity searching, I still can't figure out how to get the innerHTML of a script tag. Here's where I'm at... <script type="text/javascript" src="http://www.google.com" id="externalScript"></script ...

Tips for parsing a JSON object efficiently

Javascript var obj = { "name" : ["alex","bob","ajhoge"], "age" : [30,31,33] }; To display the value "alex," you can use: document.write(obj["name"][0]) But how can we filter through 'obj' to retrieve all data like this? html <ul ...

Comparison between a Typescript optional field and a field that has the potential to be undefined

Can you clarify the contrast between an optional field and a T | undefined field? export interface Example { property1: string | undefined property2?: string } ...

Optimal approach for creating a CSS button with a dynamic size, gradient background and arrow design

I'm utilizing the Zurb Foundation framework and aiming to create dynamic call-to-action buttons with varying sizes, text, a background gradient, and a right-pointing arrow. There are three potential approaches I've envisioned: Employ an a el ...

Creating a custom component results in an extended duration to 'eliminate' its children

1I am currently facing an issue with a time-table component created using vue.js. It contains approximately 200 nested child timeline components, making it quite complex (I wanted to share an image but lacked the reputation to do so). The main problem lie ...

Guide on using Python to save a legitimate HTML page of a YouTube video

When I use requests.get(URL, allow_redirects=True) on websites like Youtube or Reddit, instead of seeing the usual HTML text content that appears when I view the page in a browser, I get a bunch of unexecuted JavaScript. All I really need is the title of ...

The duplication and multiplication of margin-left is occurring

I am baffled by the fact that margin-left is only affecting certain elements and causing frustration. .setting-container { position: relative; top: 60px; left: 0px; width: 100%; height: calc(100% - 60px); } .setting-topnav { width: 100%; h ...