Solved

jquery onchange function problem

Posted on 2015-02-06
8
100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 how to dynamically set the form action using jQuery.
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)

756 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