Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloned element with clone(true) click not working

Tags:

html

jquery

css

I am applying clone method to clone element and append it as my requirements. All things works, but I can not click on cloned elements. I have tried to find solution but i didn't get it, please guide me to solve the issue Check my attached code

$('.circle').click(function() {
  if ($(this).closest("li").children("ol").length) {
    debugger;
    var clonediv = $(this).closest('li');
    var clndiv = $(this).parentsUntil('li').siblings('ol').children('li:eq(0)');
    var newdiv = clonediv.clone(true, true);
    newdiv.html('<div>' + clonediv.find('div').eq(0).html() + '</div>');
    newdiv.find('.circle').removeClass('circle').addClass('close');
    newdiv.insertAfter(clndiv);
  } else {
    var clonediv = $(this).parentsUntil('li');
    var clndiv = $(this).closest('ol');
    var newdiv = clndiv.clone(true, true);
    newdiv.html('<li><div>' + clonediv.html() + '</div></li>');
    newdiv.find('.circle').removeClass('circle').addClass('close');
    newdiv.insertAfter(clonediv);
  }
});
$('.close').on('click', function() {
  $(this).closest('li').remove();
  console.log('remove');
});
$('li div :header').each(function(index) {
  $("li div :header").get(index).contentEditable = "true";
});
*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: #f6f9fb;
  font-family: 'Helvetica Neue', 'Helvetica', Arial, sans-serif;
}

h1 {
  color: #fff;
  text-align: center;
}

#wrapper {
  margin: 150px auto;
  max-width: 80em;
}

#container {
  float: left;
  padding: 1em;
  width: 100%;
}

.low_pr {
  margin-top: 15px;
  position: relative;
}

.low_pr:before {
  height: 1em!important;
  left: 50%!important;
  top: -15px!important;
  width: 3px!important;
  background-color: #b7a6aa;
  content: '';
  position: absolute;
}

.ver_low_pr {
  margin-left: 15px;
  position: relative;
}

.ver_low_pr:before {
  height: 3px;
  left: -15px;
  top: 1em;
  width: 1em;
  background-color: #b7a6aa;
  content: '';
  position: absolute;
}

ol.organizational-chart,
ol.organizational-chart ol,
ol.organizational-chart li,
ol.organizational-chart li>div {
  position: relative;
}

ol.organizational-chart,
ol.organizational-chart ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

ol.organizational-chart {
  text-align: center;
}

ol.organizational-chart ol {
  padding-top: 1em;
}

ol.organizational-chart ol:before,
ol.organizational-chart ol:after,
ol.organizational-chart li:before,
ol.organizational-chart li:after,
ol.organizational-chart>li>div:before,
ol.organizational-chart>li>div:after {
  background-color: #b7a6aa;
  content: '';
  position: absolute;
}

ol.organizational-chart ol>li {
  padding: 1em 0 0 1em;
}

ol.organizational-chart>li ol:before {
  height: 1em;
  left: 50%;
  top: 0;
  width: 3px;
}

ol.organizational-chart>li ol:after {
  height: 3px;
  left: 3px;
  top: 1em;
  width: 50%;
}

ol.organizational-chart>li ol>li:not(:last-of-type):before {
  height: 3px;
  left: 0;
  top: 2em;
  width: 1em;
}

ol.organizational-chart>li ol>li:not(:last-of-type):after {
  height: 100%;
  left: 0;
  top: 0;
  width: 3px;
}

ol.organizational-chart>li ol>li:last-of-type:before {
  height: 3px;
  left: 0;
  top: 2em;
  width: 1em;
}

ol.organizational-chart>li ol>li:last-of-type:after {
  height: 2em;
  left: 0;
  top: 0;
  width: 3px;
}

ol.organizational-chart li>div {
  background-color: #fff;
  border-radius: 3px;
  min-height: 2em;
  padding: 1.5em 0.5em;
  position: relative;
  border: 1px solid #292626;
}

ol.organizational-chart li>div * {
  outline: 0;
}

ol.organizational-chart li>div .circle {
  position: absolute;
  right: -5px;
  top: -5px;
  width: 16px;
  height: 16px;
  border-radius: 100%;
  cursor: pointer;
  color: #ffffff;
  background: #292626;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
}

ol.organizational-chart li>div .close {
  position: absolute;
  right: -5px;
  top: -5px;
  width: 16px;
  height: 16px;
  border-radius: 100%;
  cursor: pointer;
  color: #ffffff;
  background: #292626;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
  transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
}


/*** PRIMARY ***/

ol.organizational-chart>li>div {
  background-color: #a2a2a2;
  margin-right: 1em;
}

ol.organizational-chart>li>div:before {
  bottom: 2em;
  height: 3px;
  right: -1em;
  width: 1em;
}

ol.organizational-chart>li>div:first-of-type:after {
  bottom: 0;
  height: 2em;
  right: -1em;
  width: 3px;
}

ol.organizational-chart>li>div+div {
  margin-top: 1em;
}

ol.organizational-chart>li>div+div:after {
  height: calc(100% + 1em);
  right: -1em;
  top: -1em;
  width: 3px;
}


/*** SECONDARY ***/

ol.organizational-chart>li>ol:before {
  left: inherit;
  right: 0;
}

ol.organizational-chart>li>ol:after {
  left: 0;
  width: 100%;
}

ol.organizational-chart>li>ol>li>div {
  background-color: #eaf5f4;
}


/*** TERTIARY ***/

ol.organizational-chart>li>ol>li>ol>li>div {
  background-color: #f5e7e8;
}


/*** QUATERNARY ***/

ol.organizational-chart>li>ol>li>ol>li>ol>li>div {
  background-color: #fff5ec;
}


/*** QUINARY ***/

ol.organizational-chart>li>ol>li>ol>li>ol>li>ol>li>div {
  background-color: #e6e5de;
}


/*** MEDIA QUERIES ***/

@media only screen and ( min-width: 64em) {
  ol.organizational-chart {
    margin-left: -1em;
    margin-right: -1em;
  }
  /* PRIMARY */
  ol.organizational-chart>li>div {
    display: inline-block;
    float: none;
    margin: 0 1em 1em 1em;
    vertical-align: bottom;
  }
  ol.organizational-chart>li>div:only-of-type {
    margin-bottom: 0;
    width: calc((50% / 1) - 2em - 4px);
  }
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(2),
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(2)~div {
    width: calc((100% / 2) - 2em - 4px);
  }
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(3),
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(3)~div {
    width: calc((100% / 3) - 2em - 4px);
  }
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(4),
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(4)~div {
    width: calc((100% / 4) - 2em - 4px);
  }
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(5),
  ol.organizational-chart>li>div:first-of-type:nth-last-of-type(5)~div {
    width: calc((100% / 5) - 2em - 4px);
  }
  ol.organizational-chart>li>div:before,
  ol.organizational-chart>li>div:after {
    bottom: -1em!important;
    top: inherit!important;
  }
  ol.organizational-chart>li>div:before {
    height: 1em!important;
    left: 50%!important;
    width: 3px!important;
  }
  ol.organizational-chart>li>div:only-of-type:after {
    display: none;
  }
  ol.organizational-chart>li>div:first-of-type:not(:only-of-type):after,
  ol.organizational-chart>li>div:last-of-type:not(:only-of-type):after {
    bottom: -1em;
    height: 3px;
    width: calc(50% + 1em + 3px);
  }
  ol.organizational-chart>li>div:first-of-type:not(:only-of-type):after {
    left: calc(50% + 3px);
  }
  ol.organizational-chart>li>div:last-of-type:not(:only-of-type):after {
    left: calc(-1em - 3px);
  }
  ol.organizational-chart>li>div+div:not(:last-of-type):after {
    height: 3px;
    left: -2em;
    width: calc(100% + 4em);
  }
  /* SECONDARY */
  ol.organizational-chart>li>ol {
    display: flex;
    display: -webkit-flex;
    display: -moz-flex;
    display: -ms-flexbox;
    flex-wrap: nowrap;
  }
  ol.organizational-chart>li>ol:before,
  ol.organizational-chart>li>ol>li:before {
    height: 1em!important;
    left: 50%!important;
    top: 0!important;
    width: 3px!important;
  }
  ol.organizational-chart>li>ol:after {
    display: none;
  }
  ol.organizational-chart>li>ol>li {
    flex-grow: 1;
    -ms-flex-grow: 1;
    -webkit-flex-grow: 1;
    padding-left: 1em;
    padding-right: 1em;
    padding-top: 1em;
  }
  ol.organizational-chart>li>ol>li:only-of-type {
    padding-top: 0;
  }
  ol.organizational-chart>li>ol>li:only-of-type:before,
  ol.organizational-chart>li>ol>li:only-of-type:after {
    display: none;
  }
  ol.organizational-chart>li>ol>li:first-of-type:not(:only-of-type):after,
  ol.organizational-chart>li>ol>li:last-of-type:not(:only-of-type):after {
    height: 3px;
    top: 0;
    width: 50%;
  }
  ol.organizational-chart>li>ol>li:first-of-type:not(:only-of-type):after {
    left: 50%;
  }
  ol.organizational-chart>li>ol>li:last-of-type:not(:only-of-type):after {
    left: 0;
  }
  ol.organizational-chart>li>ol>li+li:not(:last-of-type):after {
    height: 3px;
    left: 0;
    top: 0;
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
  <div id="container">
    <ol class="organizational-chart">
      <li>
        <div>
          <h1>
            A</h1>
          <div class="circle">
            +</div>
        </div>
        <ol>
          <li>
            <div>
              <h2>
                AA</h2>
              <div class="circle">
                +</div>
            </div>
            <ol>
              <li>
                <div>
                  <h3>
                    AAA</h3>
                  <div class="circle">
                    +</div>
                </div>
              </li>
              <li>
                <div>
                  <h3>
                    AAB</h3>
                  <div class="circle">
                    +</div>
                </div>
                <ol>
                  <li>
                    <div>
                      <h4>
                        AAAA</h4>
                      <div class="circle">
                        +</div>
                    </div>
                  </li>
                  <li>
                    <div>
                      <h4>
                        AAAB</h4>
                      <div class="circle">
                        +</div>
                    </div>
                    <ol>
                      <li>
                        <div>
                          <h5>
                            AAAAA</h5>
                          <div class="circle">
                            +</div>
                        </div>
                      </li>
                      <li>
                        <div>
                          <h5>
                            AAAAB</h5>
                          <div class="circle">
                            +</div>
                        </div>
                        <ol>
                          <li>
                            <div>
                              <h6>
                                AAAAAA</h6>
                              <div class="circle">
                                +</div>
                            </div>
                          </li>
                        </ol>
                      </li>
                    </ol>
                  </li>
                  <li>
                    <div>
                      <h4>
                        AAAC</h4>
                      <div class="circle">
                        +</div>
                    </div>
                  </li>
                </ol>
              </li>
              <li>
                <div>
                  <h3>
                    AAC</h3>
                  <div class="circle">
                    +</div>
                </div>
              </li>
            </ol>
          </li>
          <li>
            <div>
              <h2>
                BB</h2>
              <div class="circle">
                +</div>
            </div>
            <ol>
              <li>
                <div>
                  <h3>
                    Tertiary</h3>
                  <div class="circle">
                    +</div>
                </div>
                <ol>
                  <li>
                    <div>
                      <h4>
                        Quaternary</h4>
                      <div class="circle">
                        +</div>
                    </div>
                    <ol>
                      <li>
                        <div class="ver_low_pr">
                          <h5>
                            Quinary</h5>
                          <div class="circle">
                            +</div>
                        </div>
                        <ol>
                          <li>
                            <div>
                              <h6>
                                Senary</h6>
                              <div class="circle">
                                +</div>
                            </div>
                          </li>
                          <li>
                            <div class="ver_low_pr">
                              <h6>
                                Senary</h6>
                              <div class="circle">
                                +</div>
                            </div>
                          </li>
                        </ol>
                      </li>
                      <li>
                        <div>
                          <h5>
                            Quinary</h5>
                          <div class="circle">
                            +</div>
                        </div>
                      </li>
                    </ol>
                  </li>
                </ol>
              </li>
              <li>
                <div>
                  <h3>
                    Tertiary</h3>
                  <div class="circle">
                    +</div>
                </div>
              </li>
              <li>
                <div>
                  <h3>
                    Tertiary</h3>
                  <div class="circle">
                    +</div>
                </div>
              </li>
            </ol>
          </li>
        </ol>
      </li>
    </ol>
  </div>
</div>
like image 969
Hiren Vaghasiya Avatar asked Feb 28 '18 06:02

Hiren Vaghasiya


1 Answers

You need to use event-delegation

$(document).on('click','.circle',function () {

And

$(document).on('click','.close',function() {
like image 199
Anant Kumar Singh Avatar answered Oct 21 '22 06:10

Anant Kumar Singh