Solved

jquery onchange function problem

Posted on 2015-02-06
8
97 Views
Last Modified: 2015-02-06
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/
0
Comment
Question by:Isaac
  • 4
  • 3
8 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40594378
creates a copy of html elements
You obviously know what jQuery.clone() does, but you want to prevent it from "duplicating"?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40594402
Does this get you any closer?

http://jsfiddle.net/t28sv760/14/
0
 
LVL 5

Author Comment

by:Isaac
ID: 40594415
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 40594431
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
 
LVL 5

Author Comment

by:Isaac
ID: 40594445
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
 
LVL 2

Expert Comment

by:E-Risk
ID: 40594495
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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40594530
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
 
LVL 5

Author Closing Comment

by:Isaac
ID: 40594579
This works for me.  I have another similar question that I will post in a few minutes.
Thnaks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question