-1

I have a fiddle which I have replicated by seeing the screenshot below:

enter image description here

The screenshot and the fiddle is working in a way that if I hit on any of the items above the following changes happen:

  1. Background-color changes into green.
  2. Icons and Text turn into white.

The sample JQuery code which I have used in order to make that happen are:

$("#franchisehub").click(function() {
  if ($('.franchisehubtv').css('display') == "flex") {
    $('.arrow-down').css('display', 'none');
    $('.franchisehubtv').css('display', 'none');
    $('#franchisehub img.black').css('display', 'none');

  } else {
    resetAll();
    $('#franchisehub').css('background-color', 'green');
    $('#franchisehub p').css('color', 'white');
    $('.franchisehubtv').css('display', 'flex');
    $('#franchisehub img.black').css('display', 'none');
    $('#franchisehub img.white').css('display', 'inline-block');

  }

});


Problem Statement:

In the fiddle, when I hit on any of the items, the text (present in the square box) comes towards the center of the box and whereas when its not being clicked it comes outside of the square box (which I don't want to happen).

I am wondering what changes I need to make in the fiddle so that when the items are being clicked and when the items are not being clicked, it should remain at the same position.

flash
  • 1,455
  • 11
  • 61
  • 132
  • 1
    It seems that both images are showing when a box is not selected, which pushes the text down. What is in your `resetAll()` function? Also, I'd suggest using more general selectors so you don't need to repeat yourself. See [DRY @ jQuery.com](https://learn.jquery.com/code-organization/dont-repeat-yourself/). – showdev Jun 01 '18 at 18:21
  • @showdev In order to make my code modular, I have used resetAll() function so that I don't have to write of lines of code. Yes, two images get displayed, when its clicked white gets displayed and when its not clicked black gets displayed. – flash Jun 01 '18 at 18:24
  • What I mean is that *both* images are showing when a box is not selected. I assume you want to toggle between the two images. – showdev Jun 01 '18 at 18:26
  • @showdev is right. You should hide the second image, then show it when you click. – Dale Nguyen Jun 01 '18 at 18:28
  • @DaleNguyen Is there any way, we can do with the current code ? – flash Jun 01 '18 at 18:28

3 Answers3

1

This is a long answer, so I put it here.

In the original CSS, you should hide the second image in the first place

.white{
 display: none;
}

Then, in the script, you can hide the .black when you click:

 $("#franchisehub").click(function() {
      if ($('.franchisehubtv').css('display') == "flex") {
        $('.arrow-down').css('display', 'none');
        $('.franchisehubtv').css('display', 'none');
        $('#franchisehub img.white').css('display', 'inline-block');   
        $('#franchisehub img.black').css('display', 'none');

      } else {
        resetAll();
        $('#franchisehub').css('background-color', 'green');
        $('#franchisehub p').css('color', 'white');
        $('.franchisehubtv').css('display', 'flex');
        $('#franchisehub img.black').css('display', 'none');
        $('#franchisehub img.white').css('display', 'inline-block');

      }

Inside your resetAll(), you should have this too:

  $('#franchisehub img.black').css('display', 'inline-block');
  $('#franchisehub img.white').css('display', 'none');

But your code has so many repetitions, you should find a way to reduce the amount of code. This function should be shortened by a lot! https://jsfiddle.net/866bh6du/1/

Dale Nguyen
  • 1,930
  • 3
  • 24
  • 37
  • The text seems to go down again after click. – flash Jun 01 '18 at 18:48
  • 1
    I cannot do everything for you, just take a look at the first box! – Dale Nguyen Jun 01 '18 at 18:50
  • 1
    It's ok. You can apply the fix to the other boxes, and find a way to make your code clean if possible. – Dale Nguyen Jun 01 '18 at 18:53
  • I cleaned my css a lot. Haven't got time to clean jquery and html codes. Will do soon. – flash Jun 01 '18 at 18:55
  • I have used your code everywhere in the fiddle. It looks good now. Thanks for the help. I have one more quick question. In the [fiddle](https://jsfiddle.net/ekjrqnfd/embedded/result/) when the page loads, I just want the 2nd square box to have white cloud image. At this moment, it is take black cloud image on load. – flash Jun 01 '18 at 19:09
  • 1
    You just to need set the original CSS for your second box, and it'll be good. #cloudbasedmobile .white{ display: inline-block; } #cloudbasedmobile .black{ display: none; } – Dale Nguyen Jun 01 '18 at 19:32
  • I have a similar [question](https://stackoverflow.com/questions/50661303/how-to-horizontally-scroll-the-contents-in-mobile-tablet-view-in-html-css). I am wondering if you can help me out or can give me a pointer. – flash Jun 03 '18 at 06:09
0

Both images are showing when a box is not selected, which pushes the text down.
Also, I recommend not using inline CSS styles and using more general selectors to keep it DRY.

Here's an example:

jQuery('.product').on('click', function() {
  jQuery(this).toggleClass('selected');
});
.product-all-contents {
  background-color: #f0f0f0;
}

.product-contents {
  margin-left: 15%;
  margin-right: 15%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.product-contents .product {
  width: 10%;
  text-align: center;
  height: 150px;
  padding-top: 1%;
  padding-left: 1%;
  padding-right: 1%;
  border-style: solid;
  border-width: 3px;
  border-color: rgb(145, 147, 150);
  background-color: white;
  border-radius: 10px;
  font-size: 15px;
  font-family: 'Roboto';
  line-height: 1.2;
  color: rgb(58, 59, 60);
}

.product.selected {
  background-color: green;
  color: white;
}

.product img.white {
  display: none;
}
.product img.black {
  display: block;
}

.product.selected img.white {
  display: block;
}
.product.selected img.black {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="product-all-contents">
  <div class="product-contents">
    <div class="product" id="franchisehub">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/Franchise-Hub.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/franchise-hub-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">

      <p>Franchise Hub</p>
    </div>

    <div class="product" id="cloudbasedmobile">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/cloud-grey.png" alt="" width="70" height="50" class="aligncenter size-full wp-image-8042" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/cloud-based-mobile-white.png" alt="" width="70" height="50" class="aligncenter size-full wp-image-8042" />

      <p>Cloud Based & Mobile</p>
    </div>
    <div class="product" id="businessanalytics">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/business-analytics.png" alt="" width="53" height="53" class="aligncenter size-full wp-image-7949" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/business-analytics-white.png" alt="" width="53" height="53" class="aligncenter size-full wp-image-7949" />

      <p>Business Analytics</p>
    </div>
    <div class="product" id="techsupport">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/tech-support.png" alt="" width="54" height="54" class="aligncenter size-full wp-image-7953" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/tech-support-white.png" alt="" width="54" height="54" class="aligncenter size-full wp-image-7953" />

      <p>Tech Support</p>
    </div>
    <div class="product" id="ordermanagement">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/order-management.png" alt="" width="43" height="52" class="aligncenter size-full wp-image-7952" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/order-management-white.png" alt="" width="43" height="52" class="aligncenter size-full wp-image-7952" />

      <p>Order Management</p>
    </div>
    <div class="product" id="employeemanagement">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/employee-management.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/employee-management-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <p style=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-left: 5%;
         color: rgb(58, 59, 60);">Employee Management</p>
    </div>
    <div class="product" id="whitelabel">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/white-label.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/white-label-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <p>White Label</p>
    </div>
  </div>
  <div class="product-contents">
    <div class="product" id="brandcontrol">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/brand-control.png" alt="" width="57" height="57" class="aligncenter size-full wp-image-7956" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/brand-control-white.png" alt="" width="57" height="57" class="aligncenter size-full wp-image-7956" />

      <p>Brand Control</p>
    </div>
    <div class="product" id="leadtracking">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/lead-tracking.png" alt="" width="50" height="50" class="aligncenter size-full wp-image-7957" />
      <img class="white" style="display:none;" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/lead-tracking-white.png" alt="" width="50" height="50" class="aligncenter size-full wp-image-7957" />

      <p>Lead Tracking &amp; CRM</p>
    </div>
    <div class="product" id="custominvoicing">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/custom-invoicing.png" alt="" width="51" height="50" class="aligncenter size-full wp-image-7958" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/custom-invoicing-white.png" alt="" width="51" height="50" class="aligncenter size-full wp-image-7958" />

      <p>Custom Invoicing</p>
    </div>
    <div class="product" id="goalsetting">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/goal-setting.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7959" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/goal-setting-white.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7959" />

      <p>Goal Setting</p>
    </div>
    <div class="product" id="customizationtools">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/customization-tools.png" alt="" width="54" height="53" class="aligncenter size-full wp-image-7960" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/customization-tools-white.png" alt="" width="54" height="53" class="aligncenter size-full wp-image-7960" />

      <p>Customization Tools</p>
    </div>
    <div class="product" id="royaltycalculator">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/royalty-calculator.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7961" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/royalty-calculator-white.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7961" />

      <p>Royalty Calculator</p>
    </div>
    <div class="product" id="emailmarketing">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/email-marketing.png" alt="" width="51" height="52" class="aligncenter size-full wp-image-7962" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/email-marketing-white.png" alt="" width="51" height="52" class="aligncenter size-full wp-image-7962" />

      <p>Email Marketing</p>
    </div>
  </div>
</div>

Just for fun, here's an example with pure CSS and no jQuery:

.product-all-contents {
  background-color: #f0f0f0;
}

.product-contents {
  display: flex;
  justify-content: center;
  align-items: stretch;
  flex-wrap: wrap;
}

.product-contents label {
  position: relative;
  flex: 0 0 20%;
}

.product {
  height: 100%;
  width: 100%;
  text-align: center;
  font-size: 15px;
  font-family: 'Roboto';
  line-height: 1.2;
  color: rgb(58, 59, 60);
  padding: 1%;
  background-color: white;
  border: 3px solid rgb(145, 147, 150);
  border-radius: 10px;
  box-sizing: border-box;
}

.product img.white {
  display: none;
}
.product img.black {
  display: inline-block;
}

.toggler {
  display: none;
}

.toggler:checked+.product {
  background-color: green;
  color: white;
}
.toggler:checked+.product img.white {
  display: inline-block;
  ;
}
.toggler:checked+.product img.black {
  display: none;
}
<div class="product-all-contents">
  <div class="product-contents">

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/Franchise-Hub.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/franchise-hub-white.png" alt="" width="59" height="59">
        <p>Franchise Hub</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/cloud-grey.png" alt="" width="70" height="50">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/cloud-based-mobile-white.png" alt="" width="70" height="50">
        <p>Cloud Based &amp; Mobile</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/business-analytics.png" alt="" width="53" height="53">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/business-analytics-white.png" alt="" width="53" height="53">
        <p>Business Analytics</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/tech-support.png" alt="" width="54" height="54">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/tech-support-white.png" alt="" width="54" height="54">
        <p>Tech Support</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/order-management.png" alt="" width="43" height="52">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/order-management-white.png" alt="" width="43" height="52">
        <p>Order Management</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/employee-management.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/employee-management-white.png" alt="" width="59" height="59">
        <p>Employee Management</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/white-label.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/white-label-white.png" alt="" width="59" height="59">
        <p>White Label</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/brand-control.png" alt="" width="57" height="57">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/brand-control-white.png" alt="" width="57" height="57">

        <p>Brand Control</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/lead-tracking.png" alt="" width="50" height="50">
        <img class="white" style="display:none;" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/lead-tracking-white.png" alt="" width="50" height="50">
        <p>Lead Tracking &amp; CRM</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/custom-invoicing.png" alt="" width="51" height="50">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/custom-invoicing-white.png" alt="" width="51" height="50">
        <p>Custom Invoicing</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/goal-setting.png" alt="" width="50" height="51">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/goal-setting-white.png" alt="" width="50" height="51">
        <p>Goal Setting</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/customization-tools.png" alt="" width="54" height="53">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/customization-tools-white.png" alt="" width="54" height="53">
        <p>Customization Tools</p>
      </div>
    </label>

    <label>    
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/royalty-calculator.png" alt="" width="50" height="51">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/royalty-calculator-white.png" alt="" width="50" height="51">
        <p>Royalty Calculator</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/email-marketing.png" alt="" width="51" height="52">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/email-marketing-white.png" alt="" width="51" height="52">
        <p>Email Marketing</p>
      </div>
    </label>

  </div>
</div>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • It looks good now. You gave me the weekend project. Will try to use toggle class to see how it can be done. – flash Jun 01 '18 at 19:13
-1

If you can share the HTML code then it would be great, anyways here is how you can achieve this in a simple way...

$(".main-div").on('click', function() {
  $(this).css('background-color', 'green');
  $(this).css('color', 'white');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="main-div">
  Some text
</div>
<div class="main-div">
  Some text
</div>
<div class="main-div">
  Some text
</div>
showdev
  • 28,454
  • 37
  • 55
  • 73