Concealing a container element using javascript

What is the best way to hide a div that contains a nested label with a class of label.nbo-disabled-wrap using javascript? I am trying to hide all the divs with the class nbd-xlabel-wrap that have a nested label with the class nbo-disabled-wrap.

This is the script I used:

function hideLabels() {
  const labels = document.querySelectorAll('.nbo-disabled-wrap');

  labels.forEach(label => {
    let currentElement = label;
    for (let i = 0; i < 5; i++) {
      if (currentElement.parentElement) {
        currentElement = currentElement.parentElement;
      } else {
        break; // Reached the top of the DOM tree
      }
    }

    const targetDiv = currentElement.querySelector('.nbd-xlabel-wrap');
    if (targetDiv) {
      targetDiv.style.display = 'none';
    }
  });
}

hideLabels();
<div class="nbd-xlabel-wrap">
  <div class="nbd-xlabel-value">
    <div class="nbd-xlabel-value-inner" title="Titanium">
      <input ng-change="check_valid();updateMapOptions('f1539147544370')" value="1" ng-model="nbd_fields['f1539147544370'].value" name="nbd-field[f1539147544370]" type="radio" id="nbd-field-f1539147544370-1" class="ng-pristine ng-untouched ng-valid ng-not-empty">
      <label class="nbd-xlabel ng-isolate-scope nbo-disabled-wrap" for="nbd-field-f1539147544370-1" nbo-disabled="!status_fields['f1539147544370'][1].enable" nbo-disabled-type="class"></label>
    </div>
  </div>
  <label for="nbd-field-f1539147544370-1"><b>Titanium</b></label>
</div>

Answer №1

Forget about struggling with multiple parent traversal levels, just choose the elements you need and traverse them directly. Here is the updated code-

function hideLabels() {
  const wraps = document.querySelectorAll('.nbd-xlabel-wrap');

  wraps.forEach(wrap => {
    const disabledLabel = wrap.querySelector('.nbo-disabled-wrap');
    if (disabledLabel) {
      wrap.style.display = 'none';
    }
  });
}

hideLabels();

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

React: Oops! Looks like there's an issue - this.props.update is not defined as

Hello everyone, I'm diving into the world of programming for the first time and I might ask some silly questions along the way. Currently, I'm working on integrating a date picker into a search application built with React. The user should be ab ...

Ways to rearrange an object with javascript

I am looking to restructure my object by removing a nesting. How can I achieve this using JavaScript? Actual: var a = [ { clickedEvents: { 'event-element': 'a', 'event-description': & ...

When utilizing Ionic Firebase, the user profile saved in the sidemenu is stored using the Events class. However, the saved profile disappears as soon as

Hello there. I am currently working on displaying my user's information in the sidemenu, and after some research, I found that using the Events class might solve this issue. However, I have noticed that the data saved in subscribe gets destroyed whene ...

Struggling with the nested array of objects

I have utilized Mongodb aggregation and used the $facet operator to count each value of "reli" and "prov" from the collection. Here is the code I used to retrieve results from the database: const keyy = await db.aggregate([ $facet: { "reli": [ { $group ...

Toggle feature in React dropdown has a one-time activation

Is there a way to solve the issue with my React Dropdown component that closes but won't reopen properly? Could it be related to losing reference somehow? Here is the usage: https://codesandbox.io/s/react-typescript-obdgs import React, { useState, ...

The jQuery fadeOut function modifies or erases the window hash

While troubleshooting my website, I discovered the following: /* SOME my-web.com/index/#hash HERE... */ me.slides.eq(me.curID).fadeOut(me.options.fade.interval, me.options.fade.easing, function(){ /* HERE HASH IS CLEARED: my-web.com/index/# * ...

Firestore's get document method may cause an unmounted warning

I've been working on a React.js project that integrates with Firestore, and I ran into an issue where using the get method for fetching documents resulted in a "Can't perform a React state update on an unmounted component" warning. However, when ...

Cycle through the array to insert pictures into the document

var myApp = angular.module('myApp', []); myApp.controller('ng-angular-pictures', function() { console.log('inside of container'); var vm = this; vm.pictures = ['images/acapul ...

What steps can I take to create a textbox that expands as more text is

Looking to create a unique textbook design that starts out with specific width and height dimensions, but expands downward as users type beyond the initial space. Wondering if CSS can help achieve this functionality? In a standard textbox, only a scroll ba ...

Guide on accessing the final value of a text file using JavaScript

My server side script outputs results in a txt file, where the values stored look like this: 1 2 5 7 10 In order to create a real-time progress bar, I need to fetch the last value from the file using an ajax request while updating the txt file with the l ...

Expanding Form Fields with jQuery: A Step-by-Step Guide

I am having trouble figuring out how to add and remove input fields dynamically on my webpage. I have three input fields with an "+Add More" button, but I can't quite grasp the logic for adding or removing these fields. Currently, I am using HTML and ...

Increase the dimensions of the jQuery modal confirmation box

I am working on a jQuery confirmation dialog that displays some details for the user. My goal is to have the dialog adjust its dimensions after the user clicks "YES" to show a smaller text like "Please wait...". How can I dynamically change the size of the ...

Different ways of manipulating images with JavaScript

I attempted to tackle the issue independently, but I'm stumped. Is there a way to adjust the width of an image in JS when the screen dimensions are changed? ...

How can I click on an element using either the href or span substring in Protractor?

Here are the details of the element I am trying to interact with using Protractor. <hsx-nav-menu-group hsx-html-element> == $0 <hsx-nav-menu-info> <i hsx-html-element hsx-icon> <svg> <use ...

Passing an undefined value to the database via AJAX upon clicking a button

Hi there, I'm currently working on a table where I'm trying to perform an inline edit and update the value in the database by clicking on a button (an image). I've attempted to use an onclick function, but it seems to show "value=undefined&a ...

Having trouble getting AJAX to work with posting JSON data? Unsure of what steps to take next?

Hey there, can you take a look at my code? It seems to be throwing an error when I try to run it. I'm still learning, so any help would be greatly appreciated! <script type="text/javascript"> $(function() { $('#btnRegister&ap ...

Can you explain the purpose and functionality of the 'next' parameter within a middleware function in the Node.js Express framework?

Currently, I am working on Nodejs with the use of "Express js". My focus is on the implementation of "middleware functions," and here is a snippet of my existing code: const express = require('express') const app = express() ...

Updating a <div> using Ajax while also transmitting a counter variable

In my possession are two .php files. The initial file, named ajax_testing.php, is structured as follows: <!DOCTYPE html> <html> <?php $index_local=1; $_SESSION['global_index'] = $index_local; ?> <head> <script ...

JavaScript is unable to identify the operating system that is running underneath

Even though I am on a Windows system, the browser console is showing that I am using Linux. function detectOS() { const userAgent = navigator.userAgent.toLowerCase(); if (userAgent.includes('win')) { return 'Windows' ...

Switch the class of the parent div when clicked (or when ng-clicked)

I am attempting to create collapsible panels with a resize button by incorporating the following JavaScript code: function toggleSize() { $(".elementGraphic").click(function () { $(this).toggleClass("col-md-12"); $(this).toggleClass("c ...