`Trying to conceal the tr elements on button click proves tricky with jquery`

I have a pair of buttons, #btn1 and #btn2, as well as two table rows, #tr1 and #tr2. The goal is to toggle the active state of the buttons when they are clicked by the user.

The specific requirements are:

  • When the button is set to active, the elements within the corresponding tr should be displayed.
  • Conversely, when the button is marked as inactive, the elements in the respective tr must be hidden.

How can this functionality be achieved?

JQuery script for activating the buttons:

$("#btn1").click(function () {
    $(this).toggleClass("buttonActive");
});
$("#btn2").click(function () {
    $(this).toggleClass("buttonActive");
});

HTML code snippet:

<tr id="tr1">
    <td><input type="text" name="val1" id="val1"/></td>
    <td><input type="text" name="val2" id="val2"/></td>
</tr>
<tr id="tr2">
    <td>
        <input type="text" name="val3" id="val3"/>
    </td>
</tr>

Now, the objective is to hide tr1 if btn1 is not active and display it only when btn1 is active; the same logic applies to btn2.

Answer №1

Make sure to include the following two lines in each button click event.

$("#tr1").toggle();
$("#tr2").toggle();

Your customized jQuery script:

jQuery(document).ready(function(){
    $("#tr1").hide();
    $("#tr2").hide();
  $("#btn1").click(function () {
    $(this).toggleClass("buttonActive");
    $("#tr1").show();
    $("#tr2").hide();
  });
  $("#btn2").click(function () {
    $(this).toggleClass("buttonActive");
    $("#tr1").hide();
    $("#tr2").show();
  });
});

To toggle all tr elements showing/hiding, simply add this one additional line below.

$("tr").toggle();

Answer №2

To modify the appearance and functionality, make changes to the CSS and jQuery code provided below:

$(document).ready(function(){
  $('#tr1').hide();
  $('#tr2').hide();
  $('#tr3').hide();
});

$("#btn1").click(function () {
         $(this).toggleClass("buttonActive");
         if($(this).hasClass("buttonActive")){
           $('#tr1').show();
         }
         else{
           $('#tr1').hide();
         }
    });
$("#btn2").click(function () {
         $(this).toggleClass("buttonActive");
         $('#tr2').show();
         if($(this).hasClass("buttonActive")){
           $('#tr2').show();
         }
         else{
           $('#tr2').hide();
         }
    });
$("#btn3").click(function () {
         $(this).toggleClass("buttonActive");
         $('#tr3').show();
         if($(this).hasClass("buttonActive")){
           $('#tr3').show();
         }
         else{
           $('#tr3').hide();
         }
    });    
.buttonInactive {
    background-color: #1E78AB;
    border: 1px solid #1E78AB;
    color: #fff;

}
.buttonInactive.buttonActive {
    background-color:#fff;
    border: 1px solid #1E78AB;
    color: #1E78AB;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<button id="btn1" type="button" class="buttonInactive">Btn1</button>
<button id="btn2" type="button" class="buttonInactive">Btn2</button>
<button id="btn3" type="button" class="buttonInactive">Btn3</button>


<table>
  <tr id="tr1">
  <td>
  <input type="text" name="val1" id="val1" placeholder="val1"/></td>
  <td><input type="text" name="val2" id="val2" placeholder="val2"/></td>
  </tr>
  <tr id="tr2">
  <td>
  <input type="text" name="val3" id="val3" placeholder="val3"/></td>
  <td><input type="text" name="val4" id="val4" placeholder="val4"/></td>
  </tr>
  <tr id="tr3">
  <td>
  <input type="text" name="val5" id="val5" placeholder="val5"/></td>
  <td><input type="text" name="val6" id="val6" placeholder="val6"/></td>
  </tr>
</table>

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

What could be the reason for my jQuery Bootstrap form not sending properly formatted JSON to the REST service?

I have incorporated Bootstrap and jQuery into my web page. Upon a button click, a modal dialog box appears with a simple form. When submitted, the form content is sent as JSON to my RESTful web service. Take a look at the modal dialog below... <!-- M ...

Is it possible to define a multiplication margin using css?

I am trying to place a box in the center of a container, but I am unable to set a static height for the parent element as it may change. This is causing issues with aligning the box perfectly in the middle of the page. Any assistance would be greatly app ...

I need assistance improving my poor JavaScript patterns and practices. Where should I turn for help?

After focusing primarily on back-end tasks for the past few years, it's become apparent to me that JavaScript (and CoffeeScript) projects have evolved significantly in my absence. Working mainly in a rails environment, my previous JavaScript/jQuery i ...

Creating a Dynamic Slideshow on Autopilot

My JavaScript skills are not perfect and I'm feeling a bit lost. I have this code for a slideshow, but I want to make it automatic while also allowing users to navigate between images freely. I'm struggling to figure out how to implement this fun ...

Masonry ajax loading is not functioning properly

My images are supposed to load via ajax, but I am experiencing some issues. Sometimes it works perfectly fine, but most of the time they overlap each other like in this example. Where did I go wrong? Once I figure this out, I'll be able to refactor my ...

Next Value in Array Following Selected One

Apologies for the repetition, but despite my attempts, I haven't been able to find a solution that works for me. When a user clicks on an element, I need to retrieve the following information: The ID of the selected element An array containing all v ...

What is the best way to include an external JavaScript file in a Bootstrap project?

I am brand new to front-end development and I'm attempting to create an onClick() function for an element. However, it seems like the js file where the function is located is not being imported properly. I've tried following some instructions to ...

Retrieve information from MySQL database with jQuery upon button click

After fetching multiple data rows from a MySQL database and displaying them in a table, I want to include a button in the last column of each row. The unique ID for each button will be retrieved from the database. When a user clicks on any of these buttons ...

import an external JavaScript file in an HTML document

Looking to load a .js file from a simple HTML file? If you have these files in the same folder: start.js var http = require('http'); var fs = require('fs'); http.createServer(function (req, response) { fs.readFile('index.htm ...

Room on the edges of a div that is positioned absolutely

I would like to create a space of 10px on each side of the #in div, similar to this layout: Check out the code here - live demo: html <div id="out"> <div id="in"></div> </div> css #out { width: 700px; height: 40px; back ...

The issue with the <Button> component not properly linking in next.js

Having trouble with a button wrapped inside a custom Navbar component. The code snippet in question is: <NavBtn> <Link href="/contact" passHref> <Button> Contact Me </Button> </Link> & ...

Ensuring Consistent Visual Harmony Across Linked Elements

As part of my project developing an iPad app with PhoneGap and jQuery Mobile, I am looking to incorporate a preview pane within a carousel. This preview pane should display smaller versions of the other panes scaled inside it. The panes are dynamic and upd ...

Check if jQuery condition is met for classes that match exactly

The PHP echo statement below contains the code. The brackets are empty, but I can guarantee that the statement inside, which modifies CSS, works - except for one issue. I want it to only target elements with a class attribute equal to "zoom" or "zoom firs ...

How can I enhance the appearance of the WordPress pagination numbers with a stylish border

Is there a way to apply a border around the WordPress pagination that starts from the first number and ends at the final number in the pagination sequence? The current structure of the WordPress pagination includes: <a class="prev page-numbers">< ...

Incorporate an additional javascript document into a VueJS component

What is the most efficient method to include Javascript files in Vue.js components? ...

Buttons for concealment and revelation are unresponsive

I have the following code snippet for uploading an image, which is taken from a template utilizing bootstrap 2. <div class="span6"> <div class="control-group"> <label class="control-label">Imagen</label> <div cla ...

CSS - Overlapping elements on smaller screens due to being stacked

I am encountering an issue with a column of elements stacked below each other in my code. Here is a simplified version of my implementation: <div style="position: relative; height: 100%;"> <div style="position: absolute;">Really long text h ...

Is the JSON response in the AJAX request accurate, even though the content is not being refreshed?

I have a function that is triggered in the following manner: success: function(json) { if(json.valid == 1) { // Another show/hide operation that functions properly $("#div-response-" + id).html(json.message); } ...

Display the chosen date from the datepicker in the input field

Utilizing a JQuery DatePicker to be constantly displayed, I have assigned it to a div. Despite wanting it to display the selected date in the input box like in this example, it just isn't functioning as desired for me. Is there a way to have the selec ...

How to ensure the right size for your sections in HTML5

I am currently learning HTML5 and attempting to create a specific sized section in the browser where various elements like buttons and text will be displayed based on user interaction. However, I am running into an issue with adjusting the size of the sect ...