Solved

jquery onchange function problem

Posted on 2015-02-06
8
90 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now