Struggling to reveal concealed items on a webpage using jQuery and attempting to cycle through an array without success

Looking to display or hide certain sections on a page based on specific conditions. I want to only show the sections of the page that contain words from the conditionsToShow array.

function hideWorkflowConditions() {

    // initially hide the elements
    $('#descriptors_table > tbody').children().css('display', 'none');

}

function showWorkflowConditions() {

    let conditionsToShow = `
    Block transition until approval
    Category is not Empty
    Code Committed
    File Uploader is User
    File Uploader is in Group
    File Uploader is in Project Role
    Has Attachments AM
    Has Links AM
    Hide transition from user
    Limit By Status
    No Open Reviews
    Only Assignee
    Only Reporter
    Permission
    SIL
    Script  [ScriptRunner]
    Sub-Task Blocking
    Unreviewed   Code
    User Is In Group
    User Is In Group Custom Field
    User Is In Project Role
    Verify Number of Attachments in Category
    `;

    // split the conditions into an array, using line breaks
    let conditionsArray = conditionsToShow.split(/\n/).filter(Boolean);
    // remove whitespace from the array
    let conditionsArrayTrimmed = conditionsArray.map(Function.prototype.call, String.prototype.trim);

    conditionsArrayTrimmed.forEach(element => {
        // try to show the section if it contains the specified element
        $( "#descriptors_table > tbody > tr (:contains('"+ element +"'))" ).css('display', 'table-row');

    });

}

hideWorkflowConditions();
showWorkflowConditions();

// code snippet to show a single hidden element
/*var elx = "Only Reporter";
$( "#descriptors_table > tbody > tr (:contains('"+ elx +"'))" ).css('display', 'table-row');
*/

Answer №1

The selector you used is incorrect because of the parentheses around :contains(), and there is a space after the tr element, causing :contains() to target a descendant instead of the row itself.

function hideWorkflowConditions() {
  $('#descriptors_table > tbody').children().css('display', 'none');
}

function showWorkflowConditions() {
  let conditionsToShow = `
    Block transition until approval
    Category is not Empty
    Code Committed
    File Uploader is User
    File Uploader is in Group
    File Uploader is in Project Role
    Has Attachments AM
    Has Links AM
    Hide transition from user
    Limit By Status
    No Open Reviews
    Only Assignee
    Only Reporter
    Permission
    SIL
    Script  [ScriptRunner]
    Sub-Task Blocking
    Unreviewed Code
    User Is In Group
    User Is In Group Custom Field
    User Is In Project Role
    Verify Number of Attachments in Category
    `;

  // create a new array, using the new line break
  let conditionsArray = conditionsToShow.split(/\n/).filter(Boolean);
  // trim the whitespace from the array
  let conditionsArrayTrimmed = conditionsArray.map(Function.prototype.call, String.prototype.trim)
  .filter(Boolean);

  //console.log(conditionsArrayTrimmed);

  conditionsArrayTrimmed.forEach(element => {
    $("#descriptors_table > tbody > tr:contains('" + element + "')").css('display', 'table-row');

  });

}

hideWorkflowConditions();
showWorkflowConditions();

// running this works to show a single, previously hidden element
/*var elx = "Only Reporter";
$( "#descriptors_table > tbody > tr (:contains('"+ elx +"'))" ).css('display', 'table-row');
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<table id=descriptors_table>
  <tbody>
    <tr>
      <td>foo Permission
    </tr>
    <tr>
      <td>bar
    </tr>
  </tbody>
</table>

Additionally, I have included another .filter(Boolean) function to handle any whitespace strings resulting from the split operation.

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

Generating a 3D face using three coordinates in Three.js

Currently, I have implemented code that allows me to load, render, and display a STL object using Vue.js and Three.js. My goal now is to replace the currently selected plane with a new face. I've managed to extract the 3 vertices of the clicked-on fac ...

Unable to include the variable "$localStorage"

While working on my method in app.js, I encountered the following error: Uncaught Error: [$injector:strictdi] function($rootScope, $q, $localStorage, $location) is not using explicit annotation and cannot be invoked in strict mode http://errors.angula ...

Why do the async functions appear to be uncovered branches in the Jest/Istanbul coverage report?

I am encountering an issue throughout my application: When running Jest test coverage script with Istanbul, I am getting a "branch not covered" message specifically on the async function part of my well covered function. What does this message mean and how ...

Automatically populate a dropdown list based on the selection made in another dropdown menu

I'm populating a second textbox based on the input of the first textbox in auto.jsp. Now, I want to automatically populate a combo box as well. How can I achieve this? Specifically, I want to autofill the second combo box based on the selection made i ...

Alert box in Vue.js

Embarking on my journey with VueJS. I previously implemented an alert box using jQuery. Now, I am attempting to recreate that in VueJS. Here is my current progress: 1- Developed a component named NovinAlert.vue which includes: <template> & ...

The ideal version of firebase for showing messages in the foreground

Looking to instantly display notifications as soon as they are received? I recently watched a video on Angular 8 + Firebase where version 7.6.0 was discussed. While the notification is displayed in the foreground for me, I find that I need to click on some ...

React and Material-Ui utilize class definitions in .js files, which are then passed as strings to components

I am attempting to define a class within my .js file in order to utilize the material-ui theme object and pass it as a string to a component since the component's prop only accepts strings. Unfortunately, the React-Dropzone import does not accept a cl ...

Show an xeditable form that can be edited within a popup window

Looking for a way to enclose the editable form within a bootstrap uib-popover-template. Tried the editable ui-bootstrap popover method, but encountering unexpected issues. Check out Plunker 1 --> https://plnkr.co/edit/vXeVoFYVU2IU08CF Issue with angul ...

Can a simultaneous read/write conflict occur in JavaScript while browsing?

A situation has arisen where I am executing multiple (let's say four) AJAX calls using AngularJS http get, and I desire each call to invoke a callback function that increments a counter. This way, I can track when all four threads have finished. I am ...

A comprehensive guide on adjusting the height of images dynamically using only CSS

Calling all CSS experts: I'm curious if it's doable to replicate the image scaling effect seen on this website using only CSS? Please note that the images only begin scaling when the height of the browser window changes, not its width. (I menti ...

Mastering Light and Camera Selection in Three.js

Question, In the editor found at this link, you can click on a light or camera to select it. I am familiar with using raycaster.intersectObjects(objects) to select meshes, but how can I achieve the same result for lights and cameras which do not have mesh ...

AngularJS continues to display an "Invalid Request" message whenever the requested resource is unavailable

When I try to fetch a group of images through AJAX and exhibit them on the page using ng-repeat, an issue arises with the image source tags. These tags send incorrect requests to the server before the collection is retrieved, leading to error messages in t ...

Centering a button within a table-cell through movement

I'm facing an issue with aligning a button placed inside a table cell. The table setup is as follows: <b-table v-if="rows.length" :thead-tr-class="'bug-report-thead'" :tbody-tr-class="'bug-report-tbody& ...

Establish a connection to couchDB using JavaScript on the server side

I'm working on a piece of code that involves using Restify to set up a server in node.js and create specific routes. My goal is to interact with CouchDB by performing actions such as GET, POST, DELETE, and PUT. var restify = require("restify"); var s ...

How can I set up an additional "alert" for each form when making an AJAX request?

let retrieveLoginPasswords = function (retrieveForgottenPasswords, checkLoginStatus) { $(document).ready(function () { $('#login,#lostpasswordform,#register').submit(function (e) { e.preventDefault(); $.ajax({ type: &quo ...

Is it possible that an object sent as a response body from a Spring MVC application's controller cannot be fetched using the jQuery AJAX method?

After making an AJAX call, the EmployeeBean object is not being returned and no exception is being thrown. Here is the code snippet from the controller method: I am trying to return an EmployeeBean object from this method using @Responsebody @RequestMapp ...

Eliminate any unnecessary spaces in the text of a link following the breaking of a word

While working on a navigation menu, I noticed that in large screens the navigation looks fine but in laptop view, the words are breaking up and it's acceptable. However, I want to remove the extra spacing after the text. In the large screen: https:/ ...

Discovering instructions on locating Material UI component documentation

I'm having trouble locating proper documentation for MUI components. Whenever I attempt to replicate an example from the site, I struggle to customize it to fit my requirements. There are numerous props used in these examples that I can't seem to ...

Using AJAX in PHP to submit checkbox values in a form without reloading the page

Recently, I delved into learning ajax and found it to be truly amazing and a major time-saver. However, I encountered a roadblock when attempting to send form data without having the page reload. Here is an excerpt of my HTML code. <form id="form ...

The art of connecting Javascript commands in succession

I'm struggling to grasp the concept of chaining and return in JavaScript. For example, consider the following code snippet: let array = [1, 2, 3] let newArray = array.map(val => val * 10).map(val => val * 10) console.log(newArray) // [100, 200, ...