1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help cloning first child to 2nd parent

Discussion in 'JavaScript' started by neilfurry, Jul 4, 2017.

  1. #1
    Hi Mate,

    Please help me solve this problem.

    here is jfiddle:
    https://jsfiddle.net/11oLbzf6/

    I need to copy the first div child to the 2nd mother using clone.

    The first mother can have multiple children and i only want to get the first child and put it into the 2nd mother, then when on the second mother it can be clone again to produce multiple children.

    Thank you in advance.

    Neil
     
    Last edited: Jul 4, 2017
    neilfurry, Jul 4, 2017 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi Neil. For your specific structure:
    <div class='mother'>
    mother1
        <div class='child'>
          child1
        </div>
      
         <div class='child'>
        child2
        </div>
    
    </div>
    <hr>
    <div class='mother2'>
    mother2
    </div>
    
    <button>
    Copy first child to mother 2
    </button>
    HTML:
    you may want to try a way below:

    
    var button = document.getElementsByTagName('button')[0],
       buttonClickHandler = function(){
         var children = document.getElementsByClassName('child'),
              childClone = children[0].cloneNode(true),
              mother2 = document.getElementsByClassName('mother2')[0];
         mother2.appendChild(childClone);
      };
    button.addEventListener('click', buttonClickHandler, false);
    
    Code (JavaScript):
     
    hdewantara, Jul 5, 2017 IP
  3. neilfurry

    neilfurry Active Member

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #3
    Thank you mate,.

    I will try..
     
    neilfurry, Jul 5, 2017 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    You're welcome.
     
    hdewantara, Jul 5, 2017 IP