Steps to conceal a div when a particular property is detected in one of its child elements (img)

On my website, I have a label and a checkbox. The checkbox in question is created by Salesforce (thus the dynamic appearance of the span id).

This excerpt shows the code from my webpage:

<div class="fds" id="checkinput">
<label>Additional Review</label>
<span id="j_id0:asp-acc-details:as-c1">
<img src="/img/checkbox_checked.gif" alt="Not Checked" width="21" height="16" class="checkImg" title="Not Checked">
</span>
</div>

The span tag and its associated img are both dynamically generated through Salesforce.

I am wondering if it is possible to use jQuery to make the "checkinput" div hidden or invisible if the title (or alt) of the img tag reads "Not Checked".

Is this feasible?

Answer №1

Implement the find and attr methods to access the image and its title attribute. Then, utilize the hide method to conceal the div

var defaultTitle = "Unchecked";

var imageTitle = $("#checkinput").find("img").attr('title');
if (imageTitle === defaultTitle) {
  $("#checkinput").hide()

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fds" id="checkinput">
  <label>Additional Review</label>
  <span id="j_id0:asp-acc-details:as-c1">
<img src="/img/checkbox_checked.gif" alt="Not Checked" width="21" height="16" class="checkImg" title="Not Checked">
</span>
</div>

Answer №2

To check the attributes of your image, you can utilize the attr() method:

(function() {
  var img = $(".checkImg");
  if (img.attr("title") === "Not Checked" || img.attr("alt") === "Not Checked")
    $("#checkinput").hide();
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fds" id="checkinput">
  <label>Additional Review</label>
  <span id="j_id0:asp-acc-details:as-c1">
     <img src="/img/checkbox_checked.gif" alt="Not Checked" width="21" height="16" class="checkImg" title="Not Checked"/>
  </span>
</div>

Edit If you have multiple similar div elements with images and need to handle them together, try this approach:

(function() {
  $(".fds img").each(function() {
    var img = $(this);
    if (img.attr("title") === "Not Checked" || img.attr("alt") === "Not Checked")
      img.closest(".fds").hide();
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fds" id="checkinput1">
  <label>Review</label>
  <span id="j_id0:asp-acc-details:as-c1">
     <img src="/img/checkbox_checked.gif" alt="Checked" width="21" height="16" class="checkImg" title="Checked"/>
  </span>
</div>
<div class="fds" id="checkinput2">
  <label>Additional Review</label>
  <span id="j_id0:asp-acc-details:as-c2">
     <img src="/img/checkbox_checked.gif" alt="Not Checked" width="21" height="16" class="checkImg" title="Not Checked"/>
  </span>
</div>

Answer №3

Utilize jQuery $("[],[]") OR syntax for selecting based on those particular attributes.

    $( "[img[alt='Not Checked']], [img[title='Not Checked']]" ).each(function(img) {
      
      $(img).parent().parent().hide() // utilizing parent relationship to hide

      // $(img).closest(".fds").hide() // alternative method using class for hiding
      // $("#checkinput").hide(); // another option using id for hiding
    })

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

Get rid of all numbers from a jQuery selection except for the first and last

I am dealing with an array let numberArray = ["500", "600", "700", "800", "900", "1000", "1100", "1200"] My objective is to remove all elements except for the first and last ones. The challenge arises when the array contains only one value, as I must ens ...

Troubleshooting: Why Won't My JQuery Disable the Radio Buttons?

My asp.net website has a piece of code that is supposed to disable the 'Tel' and 'Email' radio buttons if the Tel or Email field is blank. However, when I visit the page, the radio buttons remain enabled. I have tried using various exte ...

How can I ensure that a list of cards is 100% in height?

Can anyone assist me in setting the height of my grid cards? I am struggling to adjust the height of my grid card list, despite setting the parent element's height as well <div class="desc-grid__body cards-grid"> <ul clas ...

Display the results of the constantly changing JSON response

I am expecting to receive a dynamic JSON response based on the provided input. Here is an example: { results: [ { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087c6d7b7c487c6d7b7c266b6765">[email protec ...

Registering components globally in Vue using the <script> tag allows for seamless integration

I'm currently diving into Vue and am interested in incorporating vue-tabs into my project. The guidelines for globally "installing" it are as follows: //in your app.js or a similar file // import Vue from 'vue'; // Already available imp ...

Currently, I am on a quest to locate a particular item within an array through the power of node.js

Here is the array I am working with: [ { "_attributes": { "key": "attributes" }, "dt_assoc": { "item": { "_attributes": { "key": "status" }, ...

Using Jquery to adjust the width of table cells within a div container

Is there a way to use JQuery to access the first table's td in this HTML code and change the width to 40% and 60% respectively? <div id="DeltaPlaceHolderMain"> <a id="mainContent" tabindex="-1" name="mainContent"></a> ...

"The JQuery .validate method is not compatible with this property or method" - error message displayed

I seem to be facing a problem that I can't seem to solve. I keep getting an error message that says "Object doesn't support this property or method" while using jquery-1.10.4. The .validate method is not functioning properly for me. Any assistanc ...

Function that returns an array

Hey there, wondering about variable scope in closures! I've come across a lot of questions on this topic but haven't found the solution to my issue. Here's the code snippet: var teams = []; var players = []; var getRoles = function(roleL ...

Obtaining information from HTML through regular expressions

Below is the HTML code snippet I am dealing with <table class="profile-stats"> <tr> <td class="stat"> <div class="statnum">8</div> <div class="statlabel"> Tweets </div> </td> <td ...

Controlling the document object model of a third-party website within the Electron framework

I am completely new to electron. My main goal is to load a URL and then execute some Javascript that will make changes to the DOM. Currently, my approach involves creating a BrowserWindow that loads the URL, and I understand that I can utilize webContents ...

Issues persist with the functionality of form validation as red error messages are not being displayed as anticipated using PHP, AJAX, jQuery,

Hey guys, I'm facing an issue with the error messages that show up during form validation. I think it has something to do with the variables $errors and $data in my PHP code or maybe a problem in the jQuery code. I followed a tutorial online to crea ...

What could be causing my HTML element to vanish once the animation is complete?

I have successfully implemented an animation on 3 HTML elements for a landing page I am currently developing. The animations are working as intended, but I am facing an issue where the two lower elements (an h1 tag and a button) briefly appear on the page ...

Determine if an option is chosen in multiple select elements using Vanilla JavaScript

In order to determine if a checkbox is checked, we use the following code: let isChecked = event.target.checked But what about multiple select options like the example below? <select name="books[]" multiple> <option value="A">A</option& ...

MSW is not effective when utilized in functions within certain contexts

Trying to intercept a post request using MSW for result mocking. A handleLogin function is located within a context, but MSW cannot recognize handleLogin as a function. An error occurs: ReferenceError: handleLogin is not a function. If the API call is made ...

Location of Custom HTML Widget in Django-Dashing

I've encountered a dilemma while using the Django-Dashing framework, specifically regarding the placement of my HTML file for a custom widget. I have meticulously configured the code in my dashboard.html file to ensure proper loading. {% extends &apo ...

Creating a dynamic mongoDB query string for field selection in a JavaScript application: A step-by-step guide

I am currently working on a Node.js application that utilizes MongoDB. The schema for my customers collection is as follows: { '_id' : 1234341264876876143 , 'profile':{ 'name': 'bob', &ap ...

Looking for some help with a quick jQuery mouseleave inquiry

I wrote a short JavaScript code using jQuery that allows for a ul to slide down when an image is clicked: $(document).ready(function () { $('img.menu_class').click(function() { $('ul.the_menu').slideToggle('medium'); }); }); ...

JS/Electron Insert items individually

Apologies if my explanation is unclear. I have a function (shown below) that parses a JSON file and creates a grid of 1550 items. How can I add them one by one instead of all at once? Loading all 1500 items together is taking too long. function addItem () ...

Having trouble extracting text from a webelement using Python's Selenium

I've encountered an obstacle with Python Selenium when trying to extract text from a web element. My system is running Python 3.6.8 and Selenium 3.141.0. Using the xpath method, I locate a web element and store it in a test variable (testvar). This v ...