If the color of the border matches and the width of the border is equal to

As I navigate through multiple divs, my goal is to identify those with a distinct blue border. Furthermore, if a main div possesses a blue border, I need to check for any inner divs that also have a blue border or a border width of 3px. If the main div has a blue border but lacks any inner divs meeting these criteria, I want to add some text dynamically.

Although my jQuery code successfully detects the main div with the blue border, it struggles to pinpoint inner divs with similar styling.

Here is the jQuery snippet I am using:

$('.decNode').each(function (e) {
        if ($(this).css('border-color') == 'rgb(0, 0, 255)') {
            console.log('There is a decNode with a blue border');
            $('div[id*=RulesBox]').each(function () { console.log($(this).css('border-width'))});
            if ($(this).find('div[id*=RulesBox]').css('border-width') == '3px') {
                console.log('There is a RulesBox with a blue border');
                $(this).find('.decNodeHeader h2').append(' - <span style="color:red;">RULES NOT MET</span>');
            }
            else {
            }                
        }
        else {
            //console.log('No decNode with a border');
        }

The structure of my HTML content looks like this:

<div id="DecNode3011" class="decNode draggable ui-draggable" style="border:3px solid rgb(0, 0, 255);">
  <div class="decNodeHeader">
    <div style="padding: 12px 12px 0px 12px;"><h2>Div Header Text</h2></div>
  </div>
  <div>
    <div id="RulesContainer7493">
              <div id="RuleSet233105">
                <div id="RulesBox233105" style="border: 1px solid #999999; background-color: #ffffff; padding-top: 8px;">
                  Rule Not Met
                </div>
              </div>
              <div id="RuleSet233106">
                <div id="RulesBox233106" style="border: 3px solid #0000FF; background-color: #ffffff; padding-top: 8px;">
                  Rule Met
                </div>
              </div>
            </div>
  </div>
</div>

Answer №1

There seems to be an issue with your jQuery code which I have now resolved and provided the corrected code below.

$('.decNode').each(function (e) {
  if ($(this).css('border-color') == 'rgb(0, 0, 255)') {
     console.log('A decNode with a blue border has been found');
     $('div[id*=RulesBox]').each(function () {                            console.log($(this).css('border-width'));
       if ($(this).css('border-width') == '3px') {
         console.log('A RulesBox with a blue border has been identified');
         $('.decNodeHeader h2').append(' - <span style="color:red;">RULES NOT MET</span>');
       }
       else {
         
       }                   
     });
    
                    
   }
   else {
            //console.log('No decNode with a border');
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="DecNode3011" class="decNode draggable ui-draggable" style="border:3px solid rgb(0, 0, 255);"">
  <div class="decNodeHeader">
    <div style="padding: 12px 12px 0px 12px;"><h2>Div Header Text</h2></div>
  </div>
  <div>
    <div id="RulesContainer7493">
              <div id="RuleSet233105">
                <div id="RulesBox233105" style="border: 1px solid #999999; background-color: #ffffff; padding-top: 8px;">
                  Rule Not Met
                </div>
              </div>
              <div id="RuleSet233106">
                <div id="RulesBox233106" style="border: 3px solid #0000FF; background-color: #ffffff; padding-top: 8px;">
                  Rule Met
                </div>
              </div>
            </div>
  </div>
</div>

Answer №2

Place the descendants of .decNode inside an iteration using .each().

In addition, one of the div tags contained an unnecessary ("") in your code.

$('.decNode').each(function(e) {
  var headTxt = $(this).find('h2');
  var ruleMet = '<span class="met" style="color: green;"> - RULES MET</span>';
  var ruleNotMet = '<span class="not-met" style="color: red;"> - RULES NOT MET</span>';
  if ($(this).css('border-color') == 'rgb(0, 0, 255)') {
    console.log('There is a decNode with blue border');
    $(this).find('div[id*=RulesBox]').each(function() {
      console.log($(this).css('border-width'));
      if ($(this).css('border-width') == '3px') {
        $(this).css('border-color', 'red');
        headTxt.append(ruleMet);
        console.log('true');
      } else {
      headTxt.append(ruleNotMet);
        console.log('false');
      }
    });
  } else {
    //console.log('No decNode with a border');
  }
  if (headTxt.find('span').hasClass('met') || headTxt.find('span').length > 1) {
  headTxt.find('.not-met:first').remove();
  }
});
span {
  content: '';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="DecNode3011" class="decNode draggable ui-draggable" style="border:3px solid rgb(0, 0, 255);">
  <div class="decNodeHeader">
    <div style="padding: 12px 12px 0px 12px;">
      <h2>Div Header Text</h2>
    </div>
  </div>
  <div>
    <div id="RulesContainer7493">
      <div id="RuleSet233105">
        <div id="RulesBox233105" style="border: 1px solid #999999; background-color: #ffffff; padding-top: 8px;">
          Rule Not Met
        </div>
      </div>
      <div id="RuleSet233106">
        <div id="RulesBox233106" style="border: 3px solid #0000FF; background-color: #ffffff; padding-top: 8px;">
          Rule Met
        </div>
      </div>
    </div>
  </div>
</div>

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

Utilize Jquery to send a preset value through an ajax request

I am working on a select box functionality where the selected option is sent via ajax to a server-side script. The current setup is functioning properly. Here is the code for the select box: <select id="main_select"> <option selecte ...

Demonstration of Concurrent Page Processing in Flask

Currently, I am working on an application that heavily utilizes graphics using libraries such as Raphael and graphdracula. The main functionality of the application involves drawing various graphs across different pages named graph1, graph2, and graph3. L ...

Experiencing an issue with Handsontable: Every time I try to run the countRows method, all I get is "undefined."

Greetings everyone! I appreciate you taking the time to read about my issue. I've been using the handsontable library for a week now and have encountered some difficulties with certain methods. Within a function, I have the following code: count = $ ...

Can you explain the distinction between ajaxComplete and beforesend when making an Ajax call with JavaScript?

Can you explain the usage of ajaxComplete and beforesend? What sets them apart from each other? Are there any alternative methods similar to success when making an Ajax call? ...

Issues with submitting a Django form using AJAX

I am experiencing an issue with the URL when trying to create a "like button" using AJAX. Whenever I click on the button, I receive a 404 error with the following URL: 'add_like'%20%%7D. The server console responds with "POST /questions/get/1/%7B ...

Explore the tree structure with AngularJS and populate it with data from a JSON

After utilizing the AngularJS TreeView example from this JSFiddle link, I encountered a peculiar issue. When attempting to retrieve data from a JSON file similar to the code provided, the tree structure appeared empty. Upon inspecting the $scope.roleList1 ...

Highlighting of Vue directives in HTML within WebStorm

Can the Vue attributes and directives be highlighted? Including those in quotes. ...

Challenges in understanding jQuery logic

Check out the Fiddle here I'm looking to duplicate an HTML structure, change the class of one element, and then paste it. Initially, the class is div-1 (Line 12 in the HTML window), and with each new append, it should increase by one number, for exa ...

Implementing the functionality to allow users to submit and delete comments using AJAX and PHP

I have implemented a php code to display all comments on any post from my MySQL database. Now, I would like to add a submit button to each comment for users to delete it without having to reload the page. I want to achieve this using AJAX but struggling wi ...

The previous button on Owl Carousel seems to be malfunctioning after navigating from a different page

In my case, I have two distinct pages: let's call them the first and second pages. On the first page, I utilize an owl carousel slider with a tag that links to a specific slide on the second page's owl carousel slider using an ID. Meanwhile, on ...

jQuery fails to display the response upon submitting JSON data

I'm encountering an issue with jQuery displaying a response after submitting data using JSON. Below is my HTML code: <input required="" width="100%" name="category_id" id="category_id" class="form-control"> <input required="" width="100%" ...

Notify immediately if there is any clicking activity detected within a designated div container

I am looking to trigger an alert when a specific div is clicked. Here is the scenario: <div class="container"> <div class="header"> <h1>Headline<h1> </div> <div class="productbox"></div> </div> I have succ ...

Unusual Behavior of jQuery Ajax StatusCode

$.get('/users/test/' + username, { statusCode: { 409: () => { valid = false; alert(username + ' is unavailable'); }, 200: () => { valid = true; } } }); When sending a requ ...

React.js Form Validation issue: When using input type "number", the text field remains a single value even after attempting to delete characters with the backspace key

Experiencing difficulty in clearing the input field value, even after pressing backspace the value remains constant. One particular value persists in the input field. import * as React from "react"; import { Button, Form } from "react-boots ...

Creating a JSON formatting structure with three layers of nesting

My goal is to create a jQuery script that will automatically populate a select box with a name and an ID based on the user's selection from another select box. However, I'm encountering difficulties in structuring my JSON file. Here are the attr ...

Error: Unable to access the property 'fontSize' as it is undefined

<!DOCTYPE HTML> <html> <head> <title>Interactive Web Page</title> <link id="mycss" rel="stylesheet" href="mycss.css"> <script> function resizeText(size) { va ...

Comparison of the "Proposed Recommendation" versus the "Candidate Recommendation"

In a recent piece about HTML5, it was mentioned that the Proposed Recommendation date is set for 2022, while the Candidate Recommendation date is from 2012. I'm curious to understand the distinction between the "Proposed Recommendation" and the "Cand ...

Finding All Initial Table Cells in jQuery

Is there a streamlined method for retrieving all td elements (cells) from every row within a specific table, or do I have to manually iterate through the collection myself? ...

Choosing from a dropdown menu by pressing keys

When I press the first letter of an option in my dropdown list, it works fine. However, if I try to press two, three, or four letters consecutively, the control vanishes. For example, if the option is 'Jquery' and I press J only, it works but pre ...

Struggling with aligning mat-icon in the center using HTML and CSS?

My issue is that the mat-icon in my generated columns is not center aligned. What could be causing this? When using ngFor to generate my datatable columns dynamically, none of them align correctly. The mat-icon inside my DIV defaults to left alignment. ...