JS - Click event not working as it should












2















Maybe some of you can help me solve this problem.
I have this code and I made like an extension for the product that will show product description when click on product. But the on click function isn't working (can't close description).
Thanks!






$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>












share|improve this question























  • Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

    – Icepickle
    Nov 18 '18 at 23:05











  • I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

    – Kristers Dzintars
    Nov 18 '18 at 23:08











  • How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

    – Icepickle
    Nov 18 '18 at 23:10











  • per product because they will have their own description

    – Kristers Dzintars
    Nov 18 '18 at 23:11











  • So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

    – Icepickle
    Nov 18 '18 at 23:13
















2















Maybe some of you can help me solve this problem.
I have this code and I made like an extension for the product that will show product description when click on product. But the on click function isn't working (can't close description).
Thanks!






$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>












share|improve this question























  • Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

    – Icepickle
    Nov 18 '18 at 23:05











  • I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

    – Kristers Dzintars
    Nov 18 '18 at 23:08











  • How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

    – Icepickle
    Nov 18 '18 at 23:10











  • per product because they will have their own description

    – Kristers Dzintars
    Nov 18 '18 at 23:11











  • So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

    – Icepickle
    Nov 18 '18 at 23:13














2












2








2








Maybe some of you can help me solve this problem.
I have this code and I made like an extension for the product that will show product description when click on product. But the on click function isn't working (can't close description).
Thanks!






$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>












share|improve this question














Maybe some of you can help me solve this problem.
I have this code and I made like an extension for the product that will show product description when click on product. But the on click function isn't working (can't close description).
Thanks!






$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>








$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>





$('.product').on('click', function(){
$('.product .productExtension').css("display","none");
$(this).children(".productExtension").css("display","block");
});
function close(){
$('.productExtension').css("display","none");
}

.product{
position: relative;
width: 80px; height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center; font-family: Arial;
}
.product > .productExtension{
position: fixed;
top: 50%; left: 50%; transform: translate(-50%,-50%);
width: 300px; height: 200px;
padding: 20px;
background: red;
text-align: left;
display: none;
}
.product > .productExtension > .closeProductExtension{
position: absolute;
top: -40px; left: 0;
width: 20px; height: 20px;
margin: 10px;
border: none;
background: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
<div class="closeProductExtension" onclick="close()">Close</div>
</div>
</div>






javascript jquery html css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 22:58









Kristers DzintarsKristers Dzintars

558




558













  • Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

    – Icepickle
    Nov 18 '18 at 23:05











  • I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

    – Kristers Dzintars
    Nov 18 '18 at 23:08











  • How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

    – Icepickle
    Nov 18 '18 at 23:10











  • per product because they will have their own description

    – Kristers Dzintars
    Nov 18 '18 at 23:11











  • So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

    – Icepickle
    Nov 18 '18 at 23:13



















  • Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

    – Icepickle
    Nov 18 '18 at 23:05











  • I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

    – Kristers Dzintars
    Nov 18 '18 at 23:08











  • How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

    – Icepickle
    Nov 18 '18 at 23:10











  • per product because they will have their own description

    – Kristers Dzintars
    Nov 18 '18 at 23:11











  • So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

    – Icepickle
    Nov 18 '18 at 23:13

















Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

– Icepickle
Nov 18 '18 at 23:05





Just as a side question, how many products with possible extension are you thinking you would have on the website? Why do you choose to go with a html click event there, but on the product not?

– Icepickle
Nov 18 '18 at 23:05













I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

– Kristers Dzintars
Nov 18 '18 at 23:08





I think there could be like 30 products or so. I don't know, because I tried with jQuery click function but it didn't work so I tried with html click event.

– Kristers Dzintars
Nov 18 '18 at 23:08













How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

– Icepickle
Nov 18 '18 at 23:10





How do you imagine your product structure will look for each product? Do you want to keep the productExtension per product or just a shared one for all products?

– Icepickle
Nov 18 '18 at 23:10













per product because they will have their own description

– Kristers Dzintars
Nov 18 '18 at 23:11





per product because they will have their own description

– Kristers Dzintars
Nov 18 '18 at 23:11













So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

– Icepickle
Nov 18 '18 at 23:13





So you are working with predefined data where the data is only available on the html? It's good to know simply how the code should behave when you have multiple products, and all different ones are clicked :)

– Icepickle
Nov 18 '18 at 23:13












4 Answers
4






active

oldest

votes


















2














Now I know it wasn't fully part of the question, and I took a bit of liberty with the styling, but there is absolutely no need to hide all different productExtension classes upon each close. It would be far lighter to read the properties detailed inside your product div, and render them to a modal.



It does have an overly complex way of closing the modal ( just some liberties at work there, I am sorry for that one :) )



The answer that is currently accepted both details the reason why you cannot use close (could be window.close), I just thought the offer a different perspective when you have more than one product how to transfer the data between a modal and your DOM as you describe it now. I think it has its advantages






window.addEventListener( 'load', function() {
document.querySelectorAll('.product').forEach( product => {
product.addEventListener('click', handleProductClicked, false );
} );

document.querySelectorAll('[data-action]').forEach( action => {
action.addEventListener('click', handleAction );
} );

function handleAction( e ) {
const owner = e.currentTarget;
const action = owner.getAttribute('data-action');
const selector = owner.getAttribute('data-target');
const target = document.querySelector( selector );
if (action === 'hide') {
if ( !target.classList.contains('hidden') ) {
target.classList.add('hidden');
}
}
}

function showModal( title, content, owner ) {
const modal = document.querySelector('#modal');
if ( modal.classList.contains('hidden') ) {
modal.classList.remove( 'hidden' );
}
modal.querySelector('[data-for=title]').innerText = title;
modal.querySelector('[data-for=content]').innerHTML = content;
}

function handleProductClicked( e ) {
const productContainer = e.currentTarget;
const name = productContainer.querySelector('.productName').innerText;
const description = productContainer.querySelector('.productExtension').innerHTML;
showModal( name, description, productContainer );
}
} );

.hidden {
display: none;
visibility: hidden;
}
.productExtension {
display: none;
visibility: hidden;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
height: 200px;
border: solid #a0a0a0 1px;
box-shadow: 5px 3px 5px #777;
background-color: #cfcfcf;
}
.modal > .title {
position: absolute;
left: 0px;
top: 0px;
right: 0px;
height: 20px;
font-size: 0.9em;
background-color: blue;
border-bottom: solid #fff 1px;
}
.modal > .title > .controls {
position: absolute;
right: 0px;
top: 0px;
width: 20px;
height: 18px;
background-color: #efefef;
border: solid #a0a0a0 1px;
text-align: center;
text-transform: small-caps;
}
.controls:hover {
font-weight: bold;
cursor: pointer;
}
.modal > .title > [data-for] {
padding: 3px;
color: #fff;
font-weight: 800;
}
.modal > .content {
position: absolute;
left: 0px;
right: 0px;
top: 21px;
bottom: 0px;
padding: 5px;
border: inset #666 1px;
}
.product {
position: relative;
width: 80px;
height: 160px;
padding: 20px;
border: solid 1px grey;
text-align: center;
font-family: Arial;
}

<div class="modal hidden" id="modal">
<div class="title">
<span data-for="title"></span>
<div class="controls">
<span data-action="hide" data-target="#modal">X</span>
</div>
</div>
<div class="content" data-for="content">
</div>
</div>
<div class="product">
<div class="productName">Red Hoodie</div>
<div class="productPrice">14.72$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in red color</div>
</div>
</div>
<div class="product">
<div class="productName">Blue Hoodie</div>
<div class="productPrice">14.75$</div>
<div class="productExtension">
<div class="productDescription">This hoodie is in blue color</div>
</div>
</div>








share|improve this answer































    1














    This is happening because both functions trigger. The first trigger because you are clicking on an item that is inside the DIV “product” and the second because you’ve passed the function to the onClick. You should take out the “productExtension” div from “product” to make it works.






    share|improve this answer
























    • Can check target also and leave structure the same

      – charlietfl
      Nov 18 '18 at 23:08



















    1














    As mentioned in other comments, you have two click handler in the parent and child. The parent div is intercepting all click events. Try this for your requirement.






    $(".product").on("click", function(e) {
    $(".product .productExtension").css("display", "none");
    $(this)
    .children(".productExtension")
    .css("display", "block");
    if (e.target.classList.contains('closeProductExtension')) {
    $(".productExtension").css("display", "none");
    }
    });








    share|improve this answer































      1














      You have several problems. The first is that you trigger the open event as well. To solve this you can use stop propagation. The second is that you are using the method name "close" which is already used in JS.






      $('.product').on('click', function() {
      $('.product .productExtension').css("display", "none");
      $(this).children(".productExtension").css("display", "block");
      });

      function closeE(event) {
      event.stopPropagation();
      $('.productExtension').css("display", "none");
      }

      .product {
      position: relative;
      width: 80px;
      height: 160px;
      padding: 20px;
      border: solid 1px grey;
      text-align: center;
      font-family: Arial;
      }

      .product>.productExtension {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 300px;
      height: 200px;
      padding: 20px;
      background: red;
      text-align: left;
      display: none;
      }

      .product>.productExtension>.closeProductExtension {
      position: absolute;
      top: -40px;
      left: 0;
      width: 20px;
      height: 20px;
      margin: 10px;
      border: 1px solid black;
      background: none;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="product">
      <div class="productName">Red Hoodie</div>
      <div class="productPrice">14.72$</div>
      <div class="productExtension">
      <div class="productDescription">This hoodie is in red color</div>
      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
      </div>
      </div>








      share|improve this answer
























      • Thanks it worked! :)

        – Kristers Dzintars
        Nov 18 '18 at 23:17











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366272%2fjs-click-event-not-working-as-it-should%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Now I know it wasn't fully part of the question, and I took a bit of liberty with the styling, but there is absolutely no need to hide all different productExtension classes upon each close. It would be far lighter to read the properties detailed inside your product div, and render them to a modal.



      It does have an overly complex way of closing the modal ( just some liberties at work there, I am sorry for that one :) )



      The answer that is currently accepted both details the reason why you cannot use close (could be window.close), I just thought the offer a different perspective when you have more than one product how to transfer the data between a modal and your DOM as you describe it now. I think it has its advantages






      window.addEventListener( 'load', function() {
      document.querySelectorAll('.product').forEach( product => {
      product.addEventListener('click', handleProductClicked, false );
      } );

      document.querySelectorAll('[data-action]').forEach( action => {
      action.addEventListener('click', handleAction );
      } );

      function handleAction( e ) {
      const owner = e.currentTarget;
      const action = owner.getAttribute('data-action');
      const selector = owner.getAttribute('data-target');
      const target = document.querySelector( selector );
      if (action === 'hide') {
      if ( !target.classList.contains('hidden') ) {
      target.classList.add('hidden');
      }
      }
      }

      function showModal( title, content, owner ) {
      const modal = document.querySelector('#modal');
      if ( modal.classList.contains('hidden') ) {
      modal.classList.remove( 'hidden' );
      }
      modal.querySelector('[data-for=title]').innerText = title;
      modal.querySelector('[data-for=content]').innerHTML = content;
      }

      function handleProductClicked( e ) {
      const productContainer = e.currentTarget;
      const name = productContainer.querySelector('.productName').innerText;
      const description = productContainer.querySelector('.productExtension').innerHTML;
      showModal( name, description, productContainer );
      }
      } );

      .hidden {
      display: none;
      visibility: hidden;
      }
      .productExtension {
      display: none;
      visibility: hidden;
      }
      .modal {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 300px;
      height: 200px;
      border: solid #a0a0a0 1px;
      box-shadow: 5px 3px 5px #777;
      background-color: #cfcfcf;
      }
      .modal > .title {
      position: absolute;
      left: 0px;
      top: 0px;
      right: 0px;
      height: 20px;
      font-size: 0.9em;
      background-color: blue;
      border-bottom: solid #fff 1px;
      }
      .modal > .title > .controls {
      position: absolute;
      right: 0px;
      top: 0px;
      width: 20px;
      height: 18px;
      background-color: #efefef;
      border: solid #a0a0a0 1px;
      text-align: center;
      text-transform: small-caps;
      }
      .controls:hover {
      font-weight: bold;
      cursor: pointer;
      }
      .modal > .title > [data-for] {
      padding: 3px;
      color: #fff;
      font-weight: 800;
      }
      .modal > .content {
      position: absolute;
      left: 0px;
      right: 0px;
      top: 21px;
      bottom: 0px;
      padding: 5px;
      border: inset #666 1px;
      }
      .product {
      position: relative;
      width: 80px;
      height: 160px;
      padding: 20px;
      border: solid 1px grey;
      text-align: center;
      font-family: Arial;
      }

      <div class="modal hidden" id="modal">
      <div class="title">
      <span data-for="title"></span>
      <div class="controls">
      <span data-action="hide" data-target="#modal">X</span>
      </div>
      </div>
      <div class="content" data-for="content">
      </div>
      </div>
      <div class="product">
      <div class="productName">Red Hoodie</div>
      <div class="productPrice">14.72$</div>
      <div class="productExtension">
      <div class="productDescription">This hoodie is in red color</div>
      </div>
      </div>
      <div class="product">
      <div class="productName">Blue Hoodie</div>
      <div class="productPrice">14.75$</div>
      <div class="productExtension">
      <div class="productDescription">This hoodie is in blue color</div>
      </div>
      </div>








      share|improve this answer




























        2














        Now I know it wasn't fully part of the question, and I took a bit of liberty with the styling, but there is absolutely no need to hide all different productExtension classes upon each close. It would be far lighter to read the properties detailed inside your product div, and render them to a modal.



        It does have an overly complex way of closing the modal ( just some liberties at work there, I am sorry for that one :) )



        The answer that is currently accepted both details the reason why you cannot use close (could be window.close), I just thought the offer a different perspective when you have more than one product how to transfer the data between a modal and your DOM as you describe it now. I think it has its advantages






        window.addEventListener( 'load', function() {
        document.querySelectorAll('.product').forEach( product => {
        product.addEventListener('click', handleProductClicked, false );
        } );

        document.querySelectorAll('[data-action]').forEach( action => {
        action.addEventListener('click', handleAction );
        } );

        function handleAction( e ) {
        const owner = e.currentTarget;
        const action = owner.getAttribute('data-action');
        const selector = owner.getAttribute('data-target');
        const target = document.querySelector( selector );
        if (action === 'hide') {
        if ( !target.classList.contains('hidden') ) {
        target.classList.add('hidden');
        }
        }
        }

        function showModal( title, content, owner ) {
        const modal = document.querySelector('#modal');
        if ( modal.classList.contains('hidden') ) {
        modal.classList.remove( 'hidden' );
        }
        modal.querySelector('[data-for=title]').innerText = title;
        modal.querySelector('[data-for=content]').innerHTML = content;
        }

        function handleProductClicked( e ) {
        const productContainer = e.currentTarget;
        const name = productContainer.querySelector('.productName').innerText;
        const description = productContainer.querySelector('.productExtension').innerHTML;
        showModal( name, description, productContainer );
        }
        } );

        .hidden {
        display: none;
        visibility: hidden;
        }
        .productExtension {
        display: none;
        visibility: hidden;
        }
        .modal {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 300px;
        height: 200px;
        border: solid #a0a0a0 1px;
        box-shadow: 5px 3px 5px #777;
        background-color: #cfcfcf;
        }
        .modal > .title {
        position: absolute;
        left: 0px;
        top: 0px;
        right: 0px;
        height: 20px;
        font-size: 0.9em;
        background-color: blue;
        border-bottom: solid #fff 1px;
        }
        .modal > .title > .controls {
        position: absolute;
        right: 0px;
        top: 0px;
        width: 20px;
        height: 18px;
        background-color: #efefef;
        border: solid #a0a0a0 1px;
        text-align: center;
        text-transform: small-caps;
        }
        .controls:hover {
        font-weight: bold;
        cursor: pointer;
        }
        .modal > .title > [data-for] {
        padding: 3px;
        color: #fff;
        font-weight: 800;
        }
        .modal > .content {
        position: absolute;
        left: 0px;
        right: 0px;
        top: 21px;
        bottom: 0px;
        padding: 5px;
        border: inset #666 1px;
        }
        .product {
        position: relative;
        width: 80px;
        height: 160px;
        padding: 20px;
        border: solid 1px grey;
        text-align: center;
        font-family: Arial;
        }

        <div class="modal hidden" id="modal">
        <div class="title">
        <span data-for="title"></span>
        <div class="controls">
        <span data-action="hide" data-target="#modal">X</span>
        </div>
        </div>
        <div class="content" data-for="content">
        </div>
        </div>
        <div class="product">
        <div class="productName">Red Hoodie</div>
        <div class="productPrice">14.72$</div>
        <div class="productExtension">
        <div class="productDescription">This hoodie is in red color</div>
        </div>
        </div>
        <div class="product">
        <div class="productName">Blue Hoodie</div>
        <div class="productPrice">14.75$</div>
        <div class="productExtension">
        <div class="productDescription">This hoodie is in blue color</div>
        </div>
        </div>








        share|improve this answer


























          2












          2








          2







          Now I know it wasn't fully part of the question, and I took a bit of liberty with the styling, but there is absolutely no need to hide all different productExtension classes upon each close. It would be far lighter to read the properties detailed inside your product div, and render them to a modal.



          It does have an overly complex way of closing the modal ( just some liberties at work there, I am sorry for that one :) )



          The answer that is currently accepted both details the reason why you cannot use close (could be window.close), I just thought the offer a different perspective when you have more than one product how to transfer the data between a modal and your DOM as you describe it now. I think it has its advantages






          window.addEventListener( 'load', function() {
          document.querySelectorAll('.product').forEach( product => {
          product.addEventListener('click', handleProductClicked, false );
          } );

          document.querySelectorAll('[data-action]').forEach( action => {
          action.addEventListener('click', handleAction );
          } );

          function handleAction( e ) {
          const owner = e.currentTarget;
          const action = owner.getAttribute('data-action');
          const selector = owner.getAttribute('data-target');
          const target = document.querySelector( selector );
          if (action === 'hide') {
          if ( !target.classList.contains('hidden') ) {
          target.classList.add('hidden');
          }
          }
          }

          function showModal( title, content, owner ) {
          const modal = document.querySelector('#modal');
          if ( modal.classList.contains('hidden') ) {
          modal.classList.remove( 'hidden' );
          }
          modal.querySelector('[data-for=title]').innerText = title;
          modal.querySelector('[data-for=content]').innerHTML = content;
          }

          function handleProductClicked( e ) {
          const productContainer = e.currentTarget;
          const name = productContainer.querySelector('.productName').innerText;
          const description = productContainer.querySelector('.productExtension').innerHTML;
          showModal( name, description, productContainer );
          }
          } );

          .hidden {
          display: none;
          visibility: hidden;
          }
          .productExtension {
          display: none;
          visibility: hidden;
          }
          .modal {
          position: fixed;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 300px;
          height: 200px;
          border: solid #a0a0a0 1px;
          box-shadow: 5px 3px 5px #777;
          background-color: #cfcfcf;
          }
          .modal > .title {
          position: absolute;
          left: 0px;
          top: 0px;
          right: 0px;
          height: 20px;
          font-size: 0.9em;
          background-color: blue;
          border-bottom: solid #fff 1px;
          }
          .modal > .title > .controls {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 20px;
          height: 18px;
          background-color: #efefef;
          border: solid #a0a0a0 1px;
          text-align: center;
          text-transform: small-caps;
          }
          .controls:hover {
          font-weight: bold;
          cursor: pointer;
          }
          .modal > .title > [data-for] {
          padding: 3px;
          color: #fff;
          font-weight: 800;
          }
          .modal > .content {
          position: absolute;
          left: 0px;
          right: 0px;
          top: 21px;
          bottom: 0px;
          padding: 5px;
          border: inset #666 1px;
          }
          .product {
          position: relative;
          width: 80px;
          height: 160px;
          padding: 20px;
          border: solid 1px grey;
          text-align: center;
          font-family: Arial;
          }

          <div class="modal hidden" id="modal">
          <div class="title">
          <span data-for="title"></span>
          <div class="controls">
          <span data-action="hide" data-target="#modal">X</span>
          </div>
          </div>
          <div class="content" data-for="content">
          </div>
          </div>
          <div class="product">
          <div class="productName">Red Hoodie</div>
          <div class="productPrice">14.72$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in red color</div>
          </div>
          </div>
          <div class="product">
          <div class="productName">Blue Hoodie</div>
          <div class="productPrice">14.75$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in blue color</div>
          </div>
          </div>








          share|improve this answer













          Now I know it wasn't fully part of the question, and I took a bit of liberty with the styling, but there is absolutely no need to hide all different productExtension classes upon each close. It would be far lighter to read the properties detailed inside your product div, and render them to a modal.



          It does have an overly complex way of closing the modal ( just some liberties at work there, I am sorry for that one :) )



          The answer that is currently accepted both details the reason why you cannot use close (could be window.close), I just thought the offer a different perspective when you have more than one product how to transfer the data between a modal and your DOM as you describe it now. I think it has its advantages






          window.addEventListener( 'load', function() {
          document.querySelectorAll('.product').forEach( product => {
          product.addEventListener('click', handleProductClicked, false );
          } );

          document.querySelectorAll('[data-action]').forEach( action => {
          action.addEventListener('click', handleAction );
          } );

          function handleAction( e ) {
          const owner = e.currentTarget;
          const action = owner.getAttribute('data-action');
          const selector = owner.getAttribute('data-target');
          const target = document.querySelector( selector );
          if (action === 'hide') {
          if ( !target.classList.contains('hidden') ) {
          target.classList.add('hidden');
          }
          }
          }

          function showModal( title, content, owner ) {
          const modal = document.querySelector('#modal');
          if ( modal.classList.contains('hidden') ) {
          modal.classList.remove( 'hidden' );
          }
          modal.querySelector('[data-for=title]').innerText = title;
          modal.querySelector('[data-for=content]').innerHTML = content;
          }

          function handleProductClicked( e ) {
          const productContainer = e.currentTarget;
          const name = productContainer.querySelector('.productName').innerText;
          const description = productContainer.querySelector('.productExtension').innerHTML;
          showModal( name, description, productContainer );
          }
          } );

          .hidden {
          display: none;
          visibility: hidden;
          }
          .productExtension {
          display: none;
          visibility: hidden;
          }
          .modal {
          position: fixed;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 300px;
          height: 200px;
          border: solid #a0a0a0 1px;
          box-shadow: 5px 3px 5px #777;
          background-color: #cfcfcf;
          }
          .modal > .title {
          position: absolute;
          left: 0px;
          top: 0px;
          right: 0px;
          height: 20px;
          font-size: 0.9em;
          background-color: blue;
          border-bottom: solid #fff 1px;
          }
          .modal > .title > .controls {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 20px;
          height: 18px;
          background-color: #efefef;
          border: solid #a0a0a0 1px;
          text-align: center;
          text-transform: small-caps;
          }
          .controls:hover {
          font-weight: bold;
          cursor: pointer;
          }
          .modal > .title > [data-for] {
          padding: 3px;
          color: #fff;
          font-weight: 800;
          }
          .modal > .content {
          position: absolute;
          left: 0px;
          right: 0px;
          top: 21px;
          bottom: 0px;
          padding: 5px;
          border: inset #666 1px;
          }
          .product {
          position: relative;
          width: 80px;
          height: 160px;
          padding: 20px;
          border: solid 1px grey;
          text-align: center;
          font-family: Arial;
          }

          <div class="modal hidden" id="modal">
          <div class="title">
          <span data-for="title"></span>
          <div class="controls">
          <span data-action="hide" data-target="#modal">X</span>
          </div>
          </div>
          <div class="content" data-for="content">
          </div>
          </div>
          <div class="product">
          <div class="productName">Red Hoodie</div>
          <div class="productPrice">14.72$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in red color</div>
          </div>
          </div>
          <div class="product">
          <div class="productName">Blue Hoodie</div>
          <div class="productPrice">14.75$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in blue color</div>
          </div>
          </div>








          window.addEventListener( 'load', function() {
          document.querySelectorAll('.product').forEach( product => {
          product.addEventListener('click', handleProductClicked, false );
          } );

          document.querySelectorAll('[data-action]').forEach( action => {
          action.addEventListener('click', handleAction );
          } );

          function handleAction( e ) {
          const owner = e.currentTarget;
          const action = owner.getAttribute('data-action');
          const selector = owner.getAttribute('data-target');
          const target = document.querySelector( selector );
          if (action === 'hide') {
          if ( !target.classList.contains('hidden') ) {
          target.classList.add('hidden');
          }
          }
          }

          function showModal( title, content, owner ) {
          const modal = document.querySelector('#modal');
          if ( modal.classList.contains('hidden') ) {
          modal.classList.remove( 'hidden' );
          }
          modal.querySelector('[data-for=title]').innerText = title;
          modal.querySelector('[data-for=content]').innerHTML = content;
          }

          function handleProductClicked( e ) {
          const productContainer = e.currentTarget;
          const name = productContainer.querySelector('.productName').innerText;
          const description = productContainer.querySelector('.productExtension').innerHTML;
          showModal( name, description, productContainer );
          }
          } );

          .hidden {
          display: none;
          visibility: hidden;
          }
          .productExtension {
          display: none;
          visibility: hidden;
          }
          .modal {
          position: fixed;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 300px;
          height: 200px;
          border: solid #a0a0a0 1px;
          box-shadow: 5px 3px 5px #777;
          background-color: #cfcfcf;
          }
          .modal > .title {
          position: absolute;
          left: 0px;
          top: 0px;
          right: 0px;
          height: 20px;
          font-size: 0.9em;
          background-color: blue;
          border-bottom: solid #fff 1px;
          }
          .modal > .title > .controls {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 20px;
          height: 18px;
          background-color: #efefef;
          border: solid #a0a0a0 1px;
          text-align: center;
          text-transform: small-caps;
          }
          .controls:hover {
          font-weight: bold;
          cursor: pointer;
          }
          .modal > .title > [data-for] {
          padding: 3px;
          color: #fff;
          font-weight: 800;
          }
          .modal > .content {
          position: absolute;
          left: 0px;
          right: 0px;
          top: 21px;
          bottom: 0px;
          padding: 5px;
          border: inset #666 1px;
          }
          .product {
          position: relative;
          width: 80px;
          height: 160px;
          padding: 20px;
          border: solid 1px grey;
          text-align: center;
          font-family: Arial;
          }

          <div class="modal hidden" id="modal">
          <div class="title">
          <span data-for="title"></span>
          <div class="controls">
          <span data-action="hide" data-target="#modal">X</span>
          </div>
          </div>
          <div class="content" data-for="content">
          </div>
          </div>
          <div class="product">
          <div class="productName">Red Hoodie</div>
          <div class="productPrice">14.72$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in red color</div>
          </div>
          </div>
          <div class="product">
          <div class="productName">Blue Hoodie</div>
          <div class="productPrice">14.75$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in blue color</div>
          </div>
          </div>





          window.addEventListener( 'load', function() {
          document.querySelectorAll('.product').forEach( product => {
          product.addEventListener('click', handleProductClicked, false );
          } );

          document.querySelectorAll('[data-action]').forEach( action => {
          action.addEventListener('click', handleAction );
          } );

          function handleAction( e ) {
          const owner = e.currentTarget;
          const action = owner.getAttribute('data-action');
          const selector = owner.getAttribute('data-target');
          const target = document.querySelector( selector );
          if (action === 'hide') {
          if ( !target.classList.contains('hidden') ) {
          target.classList.add('hidden');
          }
          }
          }

          function showModal( title, content, owner ) {
          const modal = document.querySelector('#modal');
          if ( modal.classList.contains('hidden') ) {
          modal.classList.remove( 'hidden' );
          }
          modal.querySelector('[data-for=title]').innerText = title;
          modal.querySelector('[data-for=content]').innerHTML = content;
          }

          function handleProductClicked( e ) {
          const productContainer = e.currentTarget;
          const name = productContainer.querySelector('.productName').innerText;
          const description = productContainer.querySelector('.productExtension').innerHTML;
          showModal( name, description, productContainer );
          }
          } );

          .hidden {
          display: none;
          visibility: hidden;
          }
          .productExtension {
          display: none;
          visibility: hidden;
          }
          .modal {
          position: fixed;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 300px;
          height: 200px;
          border: solid #a0a0a0 1px;
          box-shadow: 5px 3px 5px #777;
          background-color: #cfcfcf;
          }
          .modal > .title {
          position: absolute;
          left: 0px;
          top: 0px;
          right: 0px;
          height: 20px;
          font-size: 0.9em;
          background-color: blue;
          border-bottom: solid #fff 1px;
          }
          .modal > .title > .controls {
          position: absolute;
          right: 0px;
          top: 0px;
          width: 20px;
          height: 18px;
          background-color: #efefef;
          border: solid #a0a0a0 1px;
          text-align: center;
          text-transform: small-caps;
          }
          .controls:hover {
          font-weight: bold;
          cursor: pointer;
          }
          .modal > .title > [data-for] {
          padding: 3px;
          color: #fff;
          font-weight: 800;
          }
          .modal > .content {
          position: absolute;
          left: 0px;
          right: 0px;
          top: 21px;
          bottom: 0px;
          padding: 5px;
          border: inset #666 1px;
          }
          .product {
          position: relative;
          width: 80px;
          height: 160px;
          padding: 20px;
          border: solid 1px grey;
          text-align: center;
          font-family: Arial;
          }

          <div class="modal hidden" id="modal">
          <div class="title">
          <span data-for="title"></span>
          <div class="controls">
          <span data-action="hide" data-target="#modal">X</span>
          </div>
          </div>
          <div class="content" data-for="content">
          </div>
          </div>
          <div class="product">
          <div class="productName">Red Hoodie</div>
          <div class="productPrice">14.72$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in red color</div>
          </div>
          </div>
          <div class="product">
          <div class="productName">Blue Hoodie</div>
          <div class="productPrice">14.75$</div>
          <div class="productExtension">
          <div class="productDescription">This hoodie is in blue color</div>
          </div>
          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 0:04









          IcepickleIcepickle

          8,72932136




          8,72932136

























              1














              This is happening because both functions trigger. The first trigger because you are clicking on an item that is inside the DIV “product” and the second because you’ve passed the function to the onClick. You should take out the “productExtension” div from “product” to make it works.






              share|improve this answer
























              • Can check target also and leave structure the same

                – charlietfl
                Nov 18 '18 at 23:08
















              1














              This is happening because both functions trigger. The first trigger because you are clicking on an item that is inside the DIV “product” and the second because you’ve passed the function to the onClick. You should take out the “productExtension” div from “product” to make it works.






              share|improve this answer
























              • Can check target also and leave structure the same

                – charlietfl
                Nov 18 '18 at 23:08














              1












              1








              1







              This is happening because both functions trigger. The first trigger because you are clicking on an item that is inside the DIV “product” and the second because you’ve passed the function to the onClick. You should take out the “productExtension” div from “product” to make it works.






              share|improve this answer













              This is happening because both functions trigger. The first trigger because you are clicking on an item that is inside the DIV “product” and the second because you’ve passed the function to the onClick. You should take out the “productExtension” div from “product” to make it works.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 18 '18 at 23:06









              Samuel RobertoSamuel Roberto

              33817




              33817













              • Can check target also and leave structure the same

                – charlietfl
                Nov 18 '18 at 23:08



















              • Can check target also and leave structure the same

                – charlietfl
                Nov 18 '18 at 23:08

















              Can check target also and leave structure the same

              – charlietfl
              Nov 18 '18 at 23:08





              Can check target also and leave structure the same

              – charlietfl
              Nov 18 '18 at 23:08











              1














              As mentioned in other comments, you have two click handler in the parent and child. The parent div is intercepting all click events. Try this for your requirement.






              $(".product").on("click", function(e) {
              $(".product .productExtension").css("display", "none");
              $(this)
              .children(".productExtension")
              .css("display", "block");
              if (e.target.classList.contains('closeProductExtension')) {
              $(".productExtension").css("display", "none");
              }
              });








              share|improve this answer




























                1














                As mentioned in other comments, you have two click handler in the parent and child. The parent div is intercepting all click events. Try this for your requirement.






                $(".product").on("click", function(e) {
                $(".product .productExtension").css("display", "none");
                $(this)
                .children(".productExtension")
                .css("display", "block");
                if (e.target.classList.contains('closeProductExtension')) {
                $(".productExtension").css("display", "none");
                }
                });








                share|improve this answer


























                  1












                  1








                  1







                  As mentioned in other comments, you have two click handler in the parent and child. The parent div is intercepting all click events. Try this for your requirement.






                  $(".product").on("click", function(e) {
                  $(".product .productExtension").css("display", "none");
                  $(this)
                  .children(".productExtension")
                  .css("display", "block");
                  if (e.target.classList.contains('closeProductExtension')) {
                  $(".productExtension").css("display", "none");
                  }
                  });








                  share|improve this answer













                  As mentioned in other comments, you have two click handler in the parent and child. The parent div is intercepting all click events. Try this for your requirement.






                  $(".product").on("click", function(e) {
                  $(".product .productExtension").css("display", "none");
                  $(this)
                  .children(".productExtension")
                  .css("display", "block");
                  if (e.target.classList.contains('closeProductExtension')) {
                  $(".productExtension").css("display", "none");
                  }
                  });








                  $(".product").on("click", function(e) {
                  $(".product .productExtension").css("display", "none");
                  $(this)
                  .children(".productExtension")
                  .css("display", "block");
                  if (e.target.classList.contains('closeProductExtension')) {
                  $(".productExtension").css("display", "none");
                  }
                  });





                  $(".product").on("click", function(e) {
                  $(".product .productExtension").css("display", "none");
                  $(this)
                  .children(".productExtension")
                  .css("display", "block");
                  if (e.target.classList.contains('closeProductExtension')) {
                  $(".productExtension").css("display", "none");
                  }
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 '18 at 23:13









                  RedPandazRedPandaz

                  195111




                  195111























                      1














                      You have several problems. The first is that you trigger the open event as well. To solve this you can use stop propagation. The second is that you are using the method name "close" which is already used in JS.






                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>








                      share|improve this answer
























                      • Thanks it worked! :)

                        – Kristers Dzintars
                        Nov 18 '18 at 23:17
















                      1














                      You have several problems. The first is that you trigger the open event as well. To solve this you can use stop propagation. The second is that you are using the method name "close" which is already used in JS.






                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>








                      share|improve this answer
























                      • Thanks it worked! :)

                        – Kristers Dzintars
                        Nov 18 '18 at 23:17














                      1












                      1








                      1







                      You have several problems. The first is that you trigger the open event as well. To solve this you can use stop propagation. The second is that you are using the method name "close" which is already used in JS.






                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>








                      share|improve this answer













                      You have several problems. The first is that you trigger the open event as well. To solve this you can use stop propagation. The second is that you are using the method name "close" which is already used in JS.






                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>








                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>





                      $('.product').on('click', function() {
                      $('.product .productExtension').css("display", "none");
                      $(this).children(".productExtension").css("display", "block");
                      });

                      function closeE(event) {
                      event.stopPropagation();
                      $('.productExtension').css("display", "none");
                      }

                      .product {
                      position: relative;
                      width: 80px;
                      height: 160px;
                      padding: 20px;
                      border: solid 1px grey;
                      text-align: center;
                      font-family: Arial;
                      }

                      .product>.productExtension {
                      position: fixed;
                      top: 50%;
                      left: 50%;
                      transform: translate(-50%, -50%);
                      width: 300px;
                      height: 200px;
                      padding: 20px;
                      background: red;
                      text-align: left;
                      display: none;
                      }

                      .product>.productExtension>.closeProductExtension {
                      position: absolute;
                      top: -40px;
                      left: 0;
                      width: 20px;
                      height: 20px;
                      margin: 10px;
                      border: 1px solid black;
                      background: none;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="product">
                      <div class="productName">Red Hoodie</div>
                      <div class="productPrice">14.72$</div>
                      <div class="productExtension">
                      <div class="productDescription">This hoodie is in red color</div>
                      <div class="closeProductExtension" onclick="closeE(event)">Close</div>
                      </div>
                      </div>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 18 '18 at 23:15









                      Itay GalItay Gal

                      7,83652560




                      7,83652560













                      • Thanks it worked! :)

                        – Kristers Dzintars
                        Nov 18 '18 at 23:17



















                      • Thanks it worked! :)

                        – Kristers Dzintars
                        Nov 18 '18 at 23:17

















                      Thanks it worked! :)

                      – Kristers Dzintars
                      Nov 18 '18 at 23:17





                      Thanks it worked! :)

                      – Kristers Dzintars
                      Nov 18 '18 at 23:17


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366272%2fjs-click-event-not-working-as-it-should%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      鏡平學校

                      ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                      Why https connections are so slow when debugging (stepping over) in Java?