Eliminating the default inline style automatically

My goal is to hide a table row until a radio button is clicked. I attempted using display:none;, but it did not work. Upon inspecting the code in my developer tools, I noticed that the specific table row has a style attribute style="display: table-row; which I did not add and none of the other rows have.

I am uncertain about how to remove this so that I can effectively hide the row.

Below is a snippet of my code:

$(document).ready(function() {
  $('input[type="radio"]').click(function() {
    if ($(this).attr("value") == "collection") {
      $(".deliver-fee").hide('slow');
    }
    if ($(this).attr("value") == "delivery") {
      $(".deliver-fee").show('slow');
    }
  });
  $('input[type="radio"]').trigger('click');
});
.delivery-fee {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="delivery-option">
  <div class="form-check">
    <input type="radio" class="form-check-input" name="delivery-option" id="delivery" value="delivery">
    <label for="delivery" class="form-check-label">
Delivery
</label>
  </div>

  <div class="form-check">
    <input type="radio" class="form-check-input" name="delivery-option" id="collection" value="collection">
    <label for="collection" class="form-check-label">
Collection
</label>
  </div>
</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th scope="col">Product</th>
      <th scope="col">Code</th>
      <th scope="col">Quantity</th>
      <th scope="col">Unit Price</th>
      <th scope="col">Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th colspan="4">
        <div class="float-right">
          Sub Total
        </div>
      </th>
      <td>R{{ $totalPrice }}</td>
    </tr>

    <tr class="deliver-fee">
      <th colspan="4">
        <div class="float-right">
          Delivery Fee
        </div>
      </th>
      <td>R{{ $delivery }}</td>
    </tr>
  </tbody>
</table>

The functionality I intend is for the .delivery-fee row to be hidden by default when the page loads and then shown when the user clicks on the delivery option.

Answer №1

You've already hidden the item at the beginning using CSS - and it's working perfectly.

However, when you use the following code to show it:

$('input[type="radio"]').trigger('click');

The reason why the delivery fee app briefly appears and then disappears is because this code runs twice (since there are 2x input[type='radio']) - first time for delivery, showing it with .show(), and then for collection, hiding it with hide().

jQuery queues animations, which includes .hide and .show. You could use .finish(), like this:

$(".deliver-fee").finish().hide('slow');

But this would just hide the issue temporarily.

The simplest solution would be to remove that line of code and wait for the user to click. If you need the delivery fee to be shown based on preloaded information, then only run it for the :checked item like this:

$('input[type="radio"]:checked').trigger('click');

Updated code snippet:


$(document).ready(function() {
    $('input[type="radio"]').click(function() {
        if ($(this).attr("value") == "collection") {
            $(".deliver-fee").hide('slow');
        }
        if ($(this).attr("value") == "delivery") {
            $(".deliver-fee").show('slow');
        }
    });
});

.deliver-fee {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="delivery-option">
  <div class="form-check">
    <input type="radio" class="form-check-input" name="delivery-option" id="delivery" value="delivery">
    <label for="delivery" class="form-check-label">
      Delivery
    </label>
  </div>

  <div class="form-check">
    <input type="radio" class="form-check-input" name="delivery-option" id="collection" value="collection">
    <label for="collection" class="form-check-label">
       Collection
    </label>
  </div>
</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th scope="col">Product</th>
      <th scope="col">Code</th>
      <th scope="col">Quantity</th>
      <th scope="col">Unit Price</th>
      <th scope="col">Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th colspan="4">
        <div class="float-right">
          Sub Total
        </div>
      </th>
      <td>R{{ $totalPrice }}</td>
    </tr>

    <tr class="deliver-fee">
      <th colspan="4">
        <div class="float-right">
          Delivery Fee
        </div>
      </th>
      <td>R{{ $delivery }}</td>
    </tr>
  </tbody>
</table>


Regarding display:table-row - jQuery recognizes that you are calling .show() on a table row, so instead of adding display:block, it adds display:table-row.

This behavior occurs because your JavaScript code calls .show() on that tr.

Answer №2

Great job on your code! I made a small adjustment by removing the css that you had added, and now everything is working perfectly. Here is the updated code:

    $(document).ready(function(){

        $('input[type="radio"]').click(function(){
            if($(this).attr("value")=="collection"){
                $(".deliver-fee").hide('slow');
            }

            if($(this).attr("value")=="delivery"){
                $(".deliver-fee").show('slow');

            }        
        });

        $('input[type="radio"]').trigger('click');
    });
<div class="delivery-option">
    <div class="form-check">
        <input type="radio" class="form-check-input" name="delivery-option" id="delivery" value="delivery">
        <label for="delivery" class="form-check-label">
            Delivery
        </label>
    </div>

    <div class="form-check">
        <input type="radio" class="form-check-input" name="delivery-option" id="collection" value="collection">
        <label for="collection" class="form-check-label">
            Collection
        </label>
    </div>
</div>
<table class="table table-bordered">
  <thead>
    <tr>
      <th scope="col">Product</th>
      <th scope="col">Code</th>
      <th scope="col">Quantity</th>
      <th scope="col">Unit Price</th>
      <th scope="col">Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th colspan="4">
        <div class="float-right">
            Sub Total       
        </div>
      </th>
      <td>R{{ $totalPrice }}</td>
    </tr>

    <tr class="deliver-fee">
      <th colspan="4">
        <div class="float-right">
            Delivery Fee
        </div>
      </th>
      <td>R{{ $delivery }}</td>
    </tr>
  </tbody>
</table>
<script src="https://code.jquery.com/jquery-3.3.1.slim.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on displaying only the child of the current list item while hiding the children of other list items

I have multiple nested ul elements on my page that are displayed using the following jQuery code. jQuery $("li").click(function(){ $(this).children("ul").show("slow"); }); However, I want to only display one nested ul at a time. This means that when ...

Display Partial View in MVC 4 using ajax success callback

Issue: Unable to load view on ajax success. Situation: I have two cascaded dropdowns where the second dropdown's options are based on the selection of the first dropdown. Upon selecting an option in the second dropdown, I want to display a list of re ...

Implementing Pagination for a JSON Object using Javascript and Jquery

I am looking for the most effective way to implement pagination in my current situation: I am using "$('body').append(htmlText);" to display the items from a JSON object. How can I set up pagination so that each page displays only one item based ...

CSS padding not behaving as expected

Hey there, I have a question regarding adjusting text inside a div using padding. I tried applying the padding command, but it doesn't seem to have any effect. You can find the relevant part of my code here <html> <head> <meta ch ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Opacity of absolutely positioned elements

I am currently working on creating a popup box that will gray out the surrounding area. The problem I'm facing is that the opacity of the shadow div seems to be overriding that of the popup. I have tried changing the position from absolute to fixed an ...

What is the way to change the background color using HTML5 getItem?

Here is the code I am currently using: $(this).css('backgroundcolor', localStorage.getItem('bgColorr') + " !important;"); When I execute this: alert( localStorage.getItem('bgColorr') + " !important;"); I receive the correc ...

Once the ajax request is finished, load only the <script> tags that have specific ids

I'm currently implementing infinite-scroll to dynamically load more content which includes <script> tags that need to be executed. To achieve this, I have created the following code as an ajax-callback: JS on load ajax callback: function a ...

"When the screen resolution changes, the absolute positioning of images won't function as expected

I am trying to overlay one image on top of another background image, but I am facing an issue. When I change the screen resolution, the position of the image that should be placed over the background image also changes. Below is the code I am using: ...

Enhancing the smoothness of parallax scrolling

My header is going to be twice the height of the viewport. I added a simple parallax effect so that when you scroll down, it reveals the content below. However, I'm experiencing flickering in the content as I scroll, possibly due to the CSS style adju ...

The CSS properties of a div element include the :before pseudo-element with a "shape" style that does

Having trouble finding a way to neatly contain this circle inside the div without any overflow. Looking for an elegant solution! Example of desired outcome .wrapper{ background: #efefef; height: 250px; } .wrapper::before{ width: 300px; height: 300 ...

Error with ajax call in jQuery mobile app

Working on an application using PhoneGap for Android, I encountered an error while running this code block: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> ...

I'm trying to retrieve information from openweathermap in order to show it on my app, but I keep running into an error that says "Uncaught RangeError: Maximum

I recently created a div with the id 'temporary' in order to display data retrieved from the openweathermap.org API. However, I am encountering an error in the console and I'm not sure why. This is my first time working with APIs and I would ...

Styling CSS to place an image in between text and background coloring

As I try to replicate a happy accident of mine, which originally occurred during my first attempt at CSS: I was just randomly adding selectors when I stumbled upon this unique layout. However, as I proceeded with rewriting the file, I failed to save the o ...

Is there a way to always keep an element positioned directly above a fluid image that is perfectly centered?

My current project involves creating an overlay to display a fluid image of any size. The challenge I'm facing is how to consistently position the close button 30px above the image and flush with its right edge. The catch is that the container doesn&a ...

Adding a Video as a Background Button Using HTML and CSS

My attempt to utilize background-image in CSS for the button background was unsuccessful as it only supports images, not videos. Is there a way to set a video as the background of a button? My goal is to create a button with a continuously looped muted v ...

The Chrome browser is failing to detect the hover function of the Surface Pen stylus

Encountering an issue with the hover pseudo-class not functioning properly on Surface pad's Chrome browser. The hover effect is working as expected in other browsers, but not in Chrome. I am using a Surface pen to test the hover functionality. HTML: ...

Make sure to enable contentEditable so that a <br> tag is inserted

When using a contentEditable, it automatically wraps words to create a new line once the width of the editable area is reached. While this feature is useful, I am facing an issue when parsing the content afterwards as I need it to insert a <br> tag ...

The JSX element 'body' appears to be missing its closing tag

I'm currently in the process of developing a landing page using react.js. This particular page is designed for users who are not yet signed up to create an account if they wish to do so. Unfortunately, I'm encountering some errors, one of which p ...

The occurrence of an unforeseen symbol 'a' was encountered

After using jQuery to create JSON data, the following structure was produced: [{"name":"date","value":"24-05-2013"},{"name":"omschrijving","value":""}] Although this JSON is valid, I encountered an error when trying to process it with jQuery: An unexp ...