jquery onchange function problem

In the jsfiddle is code that creates a copy of html elements.  It works fine but duplicates. How can I prevent it from duplicating? What do I test for?

Also, the following line never gets executed?
$("#tabs").prepend("<ul id='tabIndex'><li>"+copyFilterBy+"</li></ul>");

Open in new window


http://jsfiddle.net/isogunro/t28sv760/13/
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
Tom BeckCommented:
The variable copyFilterBy will be out of scope unless it's global.

If you only want the text from the options inside the select, clone() is not the right function.

Here's an updated jsfiddle where I have added a second select so there's one for each possibility in the top drop down. Then I added a data-value attribute to the select whose value matches the value in the top drop down option.

Now you can create the dynamic <ul> out of the text nodes of each select's options and switch back and forth between them.

http://jsfiddle.net/t28sv760/16/
0
 
Tom BeckCommented:
creates a copy of html elements
You obviously know what jQuery.clone() does, but you want to prevent it from "duplicating"?
0
 
Tom BeckCommented:
Does this get you any closer?

http://jsfiddle.net/t28sv760/14/
0
Cloud Class® Course: 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.

 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Yes, I would like to prevent it from duplicating, if possible.  I know, sounds crazy.
Check out the onchange
http://jsfiddle.net/isogunro/t28sv760/13/
0
 
Tom BeckCommented:
So what to you want to show up in #tabs div if not a copy of .controlGroup? Do you want just the text inside the select options to show up as list items?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Yes, text inside the select options.

As I toggle back an forth, is there a way to remove what I duplicated before I add it again when the drop down is "Punctuality"?
0
 
E-RiskCommented:
Something like this might get you closer based upon you wanting to remove what you duplicated before you add it again when the drop down is "Punctuality".

      $("#DashboardToggle select").change(function()
      {
        alert("HEllo Dashboard");
            var val = $(this).find("option:selected").val();
            if(val == "GLChart")
            {
            alert("Show GLChart");
            }
            if(val == "Punctuality")
            {
                  alert("Show Punctuality");
            var copyFilterBy = $(".controlGroup").clone();
            $(copyFilterBy).appendTo("body");
            $(".controlGroup" ).first().remove();
            }
      });

alert("outside");
$("#tabs").prepend("<ul id='tabIndex'><li>"+copyFilterBy+"</li></ul>");
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
This works for me.  I have another similar question that I will post in a few minutes.
Thnaks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.