Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:43:14+00:00 2026-06-13T23:43:14+00:00

I performed various research but I din’t find a solution for my problem. I

  • 0

I performed various research but I din’t find a solution for my problem. I created a drop down select with css to change color of background, but then when I try to clone it with Javascript, the new copy doesn’t change attributes in selection so it keep the original color. Just try it, add some copy and try to change the colors.

I’m new here, i’m not very able to add code so here’s to try:

http://jsfiddle.net/gabry501/FUyA3/
or github

https://github.com/gabry501/Test-Color/blob/master/test.html

HEAD

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>


<script type="text/javascript">

function cloning() {

    var container = document.getElementById('fine');
    var clone = document.getElementById('contenitore').cloneNode(true);
    container.appendChild (clone);

}

STYLE

select option,
select {
    background-color:white;
    width:200px;
    height:200px;
}

select option[value="1"],
select.o1
{
    background-color:blue;
}

select option[value="2"],
select.o2
{
    background-color:red;
}

select option[value="3"],
select.o3
{
    background-color:orange;
}

BODY

<div style="width:1100px;
height:250px;" id="contenitore">

SCRIPT

<script>$('select[id$=-status][id^=id_item-]').children().each(
    function (){
        if($(this).val() == 0){
            $(this).css('backgroundColor','white');
        }
        if($(this).val() == 1){
            $(this).css('backgroundColor','green');
        }
        if($(this).val() == 2){
            $(this).css('backgroundColor','red');
        }
        if($(this).val() == 3){
            $(this).css('backgroundColor','orange');
        }
    }
);</script>

<script>    
$('select[id$=-status][id^=id_item-]').change(function (){
    var color = $(this).find('option:selected').val();

    $(this).removeClass('o1 o2 o3').addClass('o' + $(this).find('option:selected').val());
}).change();

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:43:16+00:00Added an answer on June 13, 2026 at 11:43 pm

    cloneNode() copies only the data of the element in question. it does not copy over the event listeners. You need to do that manually..

    Use the jQuery clone() method..

    function cloning() {
    
            var container = document.getElementById('fine');
            var clone = $(document.getElementById('contenitore')).clone(true);
            $(container).append(clone);
    
        }
    

    Check Fiddle

    PS : Also you code looks really messy . You can scale it down..

    UPDATE
    I have made a few changes to the code .

    1.) Removed the click event from HTML and added that to the script.

    2.) Removed the ID’s and replaced by a className as ID’s in a document are supposed to
    be Unique.

    3.) Removed extra styles and replaced with a simple class Name.

    4.) Better to have separate files for style and script than including it in HTML.

    5.) If you want it to work , move the styles and the script to the corresponding tags I
    have marked..

    HTML

     <!doctype html>
        <html>
           <head>
             <script type="text/javascript"  src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
            </script>
            <meta charset="UTF-8">
            <title>Untitled Document</title>
            <style type="text/css">
                // Add the Styles here
            </style>
          </head>
    
           <body>
              <div style="width:1100px; height:250px;" class="contenitore">
                 <select  name="item-0-status">
                    <option value="" disabled="disabled" class="optionGroup">SELECT 
                          COLOR</option>
                    <option value="1"> BLUE</option>
                    <option value="2"> RED </option>
                    <option value="3"> ORANGE</option>
                 </select>
              </div> <!--Contenitore -->
    
              <div id="fine"></div>
    
              <br>
              <div id="bottone">
              <input id="btn" type="button" Value="ADD">
             </div>
               <script type="text/javascript">
                   // Script Comes Here
               </script>           
    
           </body>
        </html>
    

    Javascript

    $(function() {
        $('select[name="item-0-status"]').change(function() {
            $(this).removeClass('o1 o2 o3').addClass('o' + $(this).val());
        }).change();
    
        $('#btn').on('click', function() {
            var container = document.getElementById('fine');
            var clone = $(document.getElementsByClassName('contenitore')[0]).clone(true);
            $(container).append(clone);
        });
    });​
    

    Styles

    #bottone
    {
       float:left;
       text-align:left;
       clear:left;
       margin-top:20px;
    }
    
    select option,select 
    {
       background-color:#FFF;
       width:200px;
       height:200px;
    }
    
    .o1
    {
        background-color:blue;
    }
    
    .o2
    {
       background-color:red;
    }
    
    .o3 
    {
       background-color:orange;
    }​
    

    UPDATED FIDDLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a file-oriented database of some test results performed by various users.
I performed a lot of search around this question, and do not find any
During ASP.NET precompilation of our .NET 3.5 web application, various initialization is performed in
I am not a professional programmer (my area is medical research), but I am
I have a UIElement that has various transformations performed on it (scale and translate).
I have the need to perform algorithms on various primitive types; the algorithm is
I have various class which perform save and update operations of GUI elements, it
I have various custom binary files stored in perforce and for many of the
how to performed repeated task for a definite time like for 2 hours. Means
Is there some additional logic performed by Associate() operation? I want to programmatically copy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.