Solved

appending and detaching elements jquery

Posted on 2013-12-25
8
333 Views
Last Modified: 2013-12-25
Hi Experts

http://jsfiddle.net/Qgj52/2

It works fine, but only issue is when i use the default selected, it shows in the td, how can i detach it and attach it again, it does not work. remove is also not working as it completely removes the element, hide is not option here

please guide
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39738991
I think you meant to use this link http://jsfiddle.net/Qgj52/2/

Unless somebody else understands this, I think I am confused as to what you want to do.

>how can i detach it and attach it again

Can you detail what you mean, I think I am missing something.
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39738997
the code you have seen in the fiddle it works fine, now the issue is you see the default option as "Al Sections"

What i am trying is when i select from drop down any other value, it should reflect the th and the TD which is dynamicaaly created

Now, when i again do back forth and select "all selections" again, it should not display that column. and when i again choose another value, it should start again

i tried using hide, detach, remove but it does not work as expected
0
 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 100 total points
ID: 39739008
Like this http://jsfiddle.net/Qgj52/3/ ?
If you select "all my selections" you go back to default?

$(function () {
    var firstTime = true;

    $("#myselction").change(function (e) {
        var neData = $("#myselction").val();

        if (firstTime) {
            $("table.data thead tr").append("<th>Idea</th>");
            $("table.data tr:gt(0)").append("<td>" + neData + "</td>");
          firstTime = false;
        } else {
            if (neData!="All Selections(4)"){
            $("table.data tr:gt(0) td:last-child").html(neData);
        }else{
         $("table.data th:last-child, table.data td:last-child").remove();   
        }
            
        }
    });
});

Open in new window

0
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39739015
....update....  I think this is what you want http://jsfiddle.net/Qgj52/4/

$(function () {
    var firstTime = true;

    $("#myselction").change(function (e) {
        var neData = $("#myselction").val();

        if (firstTime) {
            $("table.data thead tr").append("<th>Idea</th>");
            $("table.data tr:gt(0)").append("<td>" + neData + "</td>");
          firstTime = false;
        } else {
            if (neData!="All Selections(4)"){
            $("table.data tr:gt(0) td:last-child").html(neData);
        }else{
              var colCount = 0;
              $('tr:nth-child(1) td').each(function () {
              if ($(this).attr('colspan')) {
                  colCount += +$(this).attr('colspan');
              } else {
                 colCount++;
                }
              });
            
            if (colCount>2){
                $("table.data th:last-child, table.data td:last-child").remove(); 
             firstTime = true;
            } 
        }
            
        }
    });
});

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39739034
third one seems good, only thing is: when i elect "All selections", it should remove the td of IDEA and when i choose again the dropdown value of 100 or 104, the IDEA td should appear again
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 400 total points
ID: 39739042
Hi,
you mean something like this?
$(function () {
    var ideaColumnExists = false;

    $("#myselction").change(function (e) {
        var neData = $("#myselction").val();
        if (neData == "All Selections(4)") {
            ideaColumnExists = false;
            $("table.data th:last-child, table.data td:last-child").remove();
        } else {
            if (!ideaColumnExists) {
                $("table.data thead tr").append("<th>Idea</th>");
                $("table.data tr:gt(0)").append("<td>" + neData + "</td>");
                ideaColumnExists = true;
            } else {
                $("table.data tr:gt(0) td:last-child").html(neData);
            }
        }
    });
});

Open in new window


jsfiddle: http://jsfiddle.net/EE_RainerJ/kQXL7/

HTH and merry xmas
Rainer
0
 
LVL 16

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 39739069
Thanks Guys
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)

734 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