Go ahead and give a click to choose, select the desired item, and

I am looking to set up a scenario where I can copy items by clicking and selecting options.

Take a look at this JSFiddle to see the scene with 1 Red, 1 Blue, and 1 Green box.

#container {
    width:500px;
    height:600px;
    display:block;
}
#red {
    display:inline-block;
    width:50px;
    height:50px;
    background-color:red;
}
#blue {
    display:inline-block;
    width:50px;
    height:50px;
    background-color:blue;
}
#green {
    width:350px;
    height:350px;
    background-color:green;
}

HTML:

<div id="container">
    <div id="red"></div>
    <div id="blue"></div>
    <div id="green"></div>
</div> 

The goal is to select either the blue or red box with a single click, and when clicking inside the green box, I want to create a clone of the selected box every time I click within it.

What approach should I take to achieve this, and what keywords should I start researching for the method?

I came across this link but it didn't provide much insight:

Thank you in advance for your assistance.

Answer №1

I have come up with a solution to address this situation. It is not possible to handle it without Javascript. In my approach, I utilized jQuery and here is the method I used:

1) To determine which element the user has selected, I created a global variable called isRedOrBlue. When the user clicks on the red or blue div, I adjust this variable accordingly.

var isRedOrBlue;
$('#red').on('click', function () {
    isRedOrBlue = "red";
    // Adding border for identification
    $(this).css({
        "border": "1px solid #ccc"
    });
    // Removing for the other
    $('#blue').css({
        "border": ""
    })
});
$('#blue').on('click', function () {
    isRedOrBlue = "blue";
    // Adding border for identification
    $(this).css({
        "border": "1px solid #ccc"
    })
    // Removing for the other
    $('#red').css({
        "border": ""
    })
});

2) When I click on the green div, I check the value of isRedOrBlue variable and based on that I clone the selected element and append it to the green div.

$('#green').on('click', function () {
    if (isRedOrBlue) { // None selected
        console.log(isRedOrBlue)
        $('#' + isRedOrBlue).clone().appendTo($(this));
    }
});

JSFiddle

I hope you can comprehend the logic I implemented above.

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

Is it possible to add animated text using anime.js?

var elements = document.querySelectorAll('.content'); anime({ targets: elements, content: 100, }); <script src="https://raw.githubusercontent.com/juliangarnier/anime/master/lib/anime.min.js"></script> <span class="content"> ...

What new features have they incorporated into the latest Foxy Bingo website?

Foxy Bingo has recently unveiled a new website at www.foxybingo.com that operates as a single-page site, allowing users to click on main menu items to navigate through the content by scrolling down the page. Interestingly, when a link is clicked, the URL ...

Using XPath to interact with a button in Python with Selenium

I am attempting to access a homepage, log in, click the login button, and then click a button on the second page using python/selenium. The Xpath I used for the login button worked perfectly. The browser opens, enters user credentials, and clicks the logi ...

Let us know when the information on a different tab is updated

I have multiple tabs and I want to indicate when a change occurs on another tab that the user hasn't clicked. For example, if the user hits the run button while on the Data pane, I would like the Errors tab to change to red to show that there was a ch ...

What is the best way to incorporate saving the dark/light mode feature to local storage in my application?

The website is currently hosted on a PC, and I've been experimenting with different methods to no avail. Below are the codes that I have implemented. While everything seems to be working fine, there is one issue - whenever I refresh my browser, the mo ...

Discover Angulajs ui-view to achieve a seamless 100% full-height screen experience

I am currently working on a web app using angularjs. The index.html file contains the navigation, content, and footer sections, with ui-view responsible for changing routes accordingly. My goal is to have a full-screen background image on the main page, wh ...

The Vue Swiper does not support inline containers in my code

I am facing an issue with the layout of my simple swiper in my vue app. The containers are not inline as expected, causing the second one to appear under the first. Additionally, I am struggling with making the slider element visible only within the viewpo ...

Tips for preventing the display of PHP output such as ".,.,.,." on the HTML page

Currently, I am in the process of building a website with bootstrap and php. While most of the development is complete, I have encountered an issue where the php output is appearing in the top left corner of the page. I am looking for guidance on how to re ...

Tips for sending arguments to external JavaScript from a Django view

Is there a way to send arguments to JavaScript from a Django view? I like to keep my JavaScript and HTML code in separate files, index.html and index.js. Here is the Django view: def index(request): template = loader.get_template('index.html&ap ...

Is it Possible for TD Not to Function as a Hyperlink?

I've exhausted all topics but nothing seems to be working for me! My goal is to have a table completely covered with anchor tags, but for some reason, it's not displaying any links within the TD elements, not even the text! HTML <tab ...

Establishing a color palette for various CSS classes using a gradient color spectrum

I am looking to create a gradient color scale and define classes for it based on a range of values. My scale ranges from 0 to 8.5, with increments of 0.25. This means I have a total of 34 different colors (8.5/0.25 = 34) to cover the entire spectrum. Each ...

The HTML source code in the browser varies from the output obtained through Python requests

I've noticed a discrepancy between the HTML source code displayed in my browser and the code I receive when using Python's requests.get function. You can see the image depicting this issue at the following link: Any thoughts on why this might be ...

Listening to a sequence of mp3 tracks consecutively

I'm currently working on a project that involves playing a series of mp3 files consecutively. However, I've encountered an issue with my code snippet below: Here's the code snippet: function playAudio(){ var au = document.getElementById( ...

My website appears distorted when the browser is zoomed out. What could be causing this issue?

Whenever I try to zoom out using Internet Explorer, the page doesn't zoom uniformly and ends up looking messy. Can someone please help me understand why this is happening? Here's how the page looks after zooming out (take a look at the three box ...

What is the best way to analyze and contrast the numeric values of two innerHTML elements?

As I delve into learning Javascript, I am seeking assistance in comparing numerical values of elements. Below is the code snippet that I currently have: if (parseInt(document.getElementById("player1").innerHTML) > parseInt(document.getElementById("pla ...

Style the content in the TD to be aligned to the left only when an INPUT element

Can CSS be used to left-align the contents of a TD cell when it contains an Input? Specifically, can the Input itself be left-aligned? <td> <input ....Left Align Me... /></td> ...

What sets apart the HTML tags for embed, object, and video elements?

Can you explain the technical distinctions among these three tags and provide reasons why one might be chosen over another? Additionally, why did the embed tag lose popularity in HTML 4 and get replaced by object, only to see a resurgence in HTML 5 while ...

Troubleshooting a Label Overlapping Problem in Materialize CSS

I've been working on a Materialize project and encountering an issue with pre-filled values in text boxes. Here is a snapshot of the problem: https://i.stack.imgur.com/u13jT.png <div class="input-field col s12 m6"> <input class="" id="us ...

A collection of items that mysteriously affix themselves to the top of the page as

Unique Context In my webpage, I have a specific div element with the CSS property of overflow: auto. Within this scrolling div, there is structured content like this: <h3>Group A</h3> <ul> <li>Item 1</li> <li>I ...

Creating multiple rectangles and filling them with a color chosen by the user in Angular can be achieved by following these steps

Looking for advice on angular coding. I'm working on a project that requires creating multiple rectangles and filling them based on selected values. Can anyone suggest a module that would offer this functionality? ...