1

This is my first post here.

I'm trying to make a doll-dress-up-thingy, and I'm a bit stuck.

Basically:

  • User clicks img in one of the 4 categories → It and its' parent is cloned to #base and "wearing" tab. (This works)
  • User clicks on the cloned div in the "wearing" tab, and it gets removed from #base and the "wearing" tab (but the original, non-cloned div stays) (This does not work. Nothing happens.)

This is what I have so far: https://jsfiddle.net/v0yefez5/14/

 $( ".categoryButton" ).on( "click", function() {
   $( ".ThisOneIsActive" ).removeClass( "ThisOneIsActive" );
   $( this ).addClass( "ThisOneIsActive" );
   var CategoryNumber = $( ".categoryButton" ).index( this );
   var CategoryID = "c" + CategoryNumber;
   $( "#c" + CategoryNumber ).addClass( "ThisOneIsActive" );
 } );


$( ".window img" ).click( function() {
   var aide = $( this ).attr( 'id' );
   //console.log( aide );
   if ( $( this ).hasClass( "onDoll" ) ) {
     $( this ).removeClass( "onDoll" );
     $( "#base" ).find( "#" + aide ).remove();
     $( ".items_on_doll" ).find( "#" + aide ).parent().remove();
     //console.log( "REMOVED FROM DOLL" );
   }
   else {
     $( this ).addClass( "onDoll" );
     $( this ).clone().appendTo( "#base" );
     $( this ).parent().clone().prependTo( ".items_on_doll" );
     //console.log( "ADDED TO DOLL" );
   }
 } );
html,
body {
  font-family: sans-serif;
  margin: 0;
  width: 100%;
  height: 100%;
}

h1 {
  font-size: 48px;
}

.container {
  background: pink;
  width: 700px;
  height: 330px;
  padding: 10px;
}

#base {
  width: 200px;
  height: 330px;
  background: white;
  float: left;
  position: relative;
}

#base img {
  pointer-events: none;
}

#wardrobe_content img[class$="onDoll"] {
  background: red;
}

#roulette {
  float: right;
  width: 400px;
  height: 330px;
  background: yellow;
}

#categoryBar p {
  display: inline-block;
  padding: 7px;
  background: pink;
  margin: 0;
}

p.activeCategory {
  color: #f00;
}

#base img.onDoll {
  position: absolute;
  top: 0;
  left: 0;
}

.window img.onDoll {
  position: absolute;
  top: 0;
  left: 0;
}

.window {
  position: relative;
  background: pink;
  overflow: hidden;
  margin: 5px;
  cursor: pointer;
  background-image: url(https://i.imgur.com/FBCF5Mq.png);
}

.window.hair {
  width: 80px;
  height: 100px;
  background-position: -62px -30px;
}

.window.hair img {
  position: absolute;
  left: -62px;
  top: -30px;
}

.window.skin {
  height: 251px;
  background-position: -48px -49px;
  width: 90px;
}

.window.skin img {
  position: absolute;
  left: -48px;
  top: -49px;
}

.items_on_doll .window {
  background: red;
}

.categoryButton {
  background: pink;
  cursor: pointer;
  padding: 7px;
  display: inline-block;
  margin: 2px;
}

.categoryButton#youreWearing {
  float: left;
  margin-left: -75px;
}

.categoryButton:hover {
  background: red;
}

.categoryButton.ThisOneIsActive {
  background: red;
}

.categoryContent,
.items_on_doll {
  display: none;
}

.categoryContent.ThisOneIsActive,
.items_on_doll.ThisOneIsActive {
  display: block;
  height: 294px;
  overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Clothing mixer</h1>
<div class="container">
  <div id="avatar_box">
    <div id="base"><img src="https://i.imgur.com/FBCF5Mq.png"></div>
  </div>
  <div id="roulette">
    <div id="categoryBar">
      <div class="categoryButton ThisOneIsActive">
        Hair
      </div>
      <div class="categoryButton">
        Skin
      </div>
      <div class="categoryButton">
        Pants
      </div>
      <div class="categoryButton">
        Skirt
      </div>
      <div class="categoryButton" id="youreWearing">
        Wearing
      </div>
    </div>
    <div id="wardrobe_content">
      <div class="categoryContent ThisOneIsActive" id="c0">
        <div class="window hair"><img class="category hair" id="item_id_1" src="https://i.imgur.com/k0TXwzN.png"></div>
        <div class="window hair"><img class="category hair" id="item_id_2" src="https://i.imgur.com/ZdrNHKy.png"></div>
      </div>
      <div class="categoryContent" id="c1">
        <div class="window skin"><img class="category skin" id="item_id_3" src="https://i.imgur.com/lf8WdTw.png"></div>
      </div>
      <div class="categoryContent" id="c2">
        boxes of items pants
      </div>
      <div class="categoryContent" id="c3">
        boxes of items skirts
      </div>
    </div>
    <div class="items_on_doll" id="c4">
      &nbsp;
    </div>
  </div>
</div>
  • 2
    do you have any elements with class `.window`? otherwise, it looks like a typo and it should be just `window` in the selector `$(".window img" ).click( function() {...});` – blurfus Jun 06 '17 at 18:36
  • Yes, it's the parent of each image. On the "hair" tab, the class is called "window hair" – Excited Fish Jun 06 '17 at 18:39
  • ok, then replace your selector for this: `$(".container").on("click", ".window img", function() {...}` – blurfus Jun 06 '17 at 18:43
  • 1
    Oh, changing the selector solved it! Thank you very much. I don't quite understand the other question at my current skill level. – Excited Fish Jun 06 '17 at 18:48
  • So what happens is, when the page loads, the JS code binds the event handler to the elements that match the selector *at that time of execution*. When you create a new element (i.e. brand new or clone an existing one) the HTML is duplicated but the event handler/binding does not go with it. In other words, the new elements do not have event handlers bound to them. Associating the event handler to a parent, static element (that targets a child element) allows you to do this – blurfus Jun 06 '17 at 18:53
  • BTW, Welcome to StackOverflow! – blurfus Jun 06 '17 at 18:54
  • 1
    Thank you! You've been very helpful (: – Excited Fish Jun 06 '17 at 18:56
  • You're welcome, glad I could help ;) – blurfus Jun 06 '17 at 18:56

0 Answers0