Conceal an <article> element if its src attribute is blank using jQuery

Is there a way to hide articles with empty images?

<article data-stars="<?php echo $StarRating[6];?>" name="<?php echo $Price[6];?>" class="box">
<img width="270" height="160" alt="" src="<?php echo $img[6][1];?>"></a>
<div>Content</div>
</article>

I am sorting these articles using two jQuery filters: Price Filter:

tjq(document).ready(function() {
      tjq("#price-range").slider({
          range: true,
          min: 0,
          max: 3000,
          values: [ 1, 2000 ],
          slide: function( event, ui ) {
              tjq(".min-price-label").html( "€" + ui.values[ 0 ]);
              tjq(".max-price-label").html( "€" + ui.values[ 1 ]);

              $('article[data-price]').each(function() {
                  var dPrice = $(this).attr('data-price');
                  if ( dPrice < ui.values[ 0 ] || dPrice > ui.values[ 1 ] ) 
                      $(this).hide();
                  else
                      $(this).show();
              });
          }
      });

And the Star rating filter

 $('input[type=checkbox]').click(function(e){
  $("input[type=checkbox]").each(function(){
  grabVal = $(this).val();
          if(!$(this).is(":checked")){
  $("article[data-stars='"+grabVal+"']").hide();
          }else{
  $("article[data-stars='"+grabVal+"']").show();
          }
      });

          if(!$("input[type=checkbox]").is(":checked")){
  $("article").show();   
      }
  });

Finally, for more control over the display of articles, you can use the Show less/more filter:

$(document).ready(function () {
  $('#hotel-list article:gt(4)').hide();
  $('#loadMore').click(function () {
  $('#hotel-list article:hidden:lt(2)').show();
      });
  $('#showLess').click(function () {
  $('#hotel-list article').not(':lt(4)').hide();
      });
  });

Would appreciate assistance in creating a filter to hide articles with empty images.

Answer №1

Check out this JSFiddle I created.

This code allows you to hide the element if the image source is empty.

I added a specific class to the element, and with this snippet of code you have full control on what action to take.

    $('.myClass').each(function(){
       if($(this).attr('src') == ""){
          $(this).closest('article').hide();
       }
    });

Access the Fiddle here

Answer №2

Is this the most efficient method for concealing articles that lack images?

$("article img[src=""]").each(function() {
    $(this).parent().hide();
});

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

Establish a vertical column that matches the height of its parent element

Is there a way to create a column that matches the height of its parent element? I have a navbar followed by a container with three columns. The challenge is to make the last right column the same height as its parent and also scrollable. In this scenario, ...

Accessing specific route parameters in a Vue.js application can be easily achieved by following these steps

I'm dealing with a situation involving nested route settings let routes = [ { name: 'Admin', path: '/prod/:id', component: Admin, meta: { requiresAuth: true, title: 'Admin - Fraud tool' ...

The swipe function of Hammer.js is unable to detect gestures on a rotated iframe

After creating a rotated iframe on the page using CSS transforms, I attempted to implement swipe events within the iframe. body.rotate iframe { transform: rotate(90deg); transform-origin: top right; position: absolute; t ...

What steps can I take to improve the functionality of the slide navigation feature

I am currently working on implementing a sliding menu feature. The menu can slide open smoothly, however, I am encountering an issue when trying to close it by clicking on the 'x' button. let openNav = document.querySelector(".slideOpen"); ...

Tutorial on displaying historical pricing data on a chart utilizing props in Vue Chartkick

I recently discovered a chart library called Chartkick for Vue projects. If you're interested, you can check it out here: In my project, I have data passed as props to a component. However, I encountered an issue where the prop couldn't be acces ...

Discover the hyperlink and submit it via the form to the designated email address

Is it possible to create a basic HTML form that captures name and email details, then uses jQuery to locate a specific link on the page and includes this link along with the form information in a submission to a web server? From there, a script could send ...

Using HTML5 input type number along with a regular expression pattern

In order to display only numbers and forward slashes in the input field with regex pattern, I attempted the following code: <input type="number" pattern="[0-9\/]*"> However, this code only shows the number pad from 0 to 9. How can I modify the ...

AngularJS seems to be failing to display the initial option in the input select field

When using Angularjs, binding an input select to the model results in a new empty option being created <option value="? undefined:undefined ?"></option> Here is an example of the code: <select name="category" ng-model="hotspot.category"&g ...

I need to securely store HTML formatted strings in a database

Currently, I am using a react quill component that outputs HTML content like this: EXAMPLE: <p><br></p><p>fsdafsd</p><p>fsdfsda</p><p>fsdafsad</p I want to store this content in a database. However, I ...

Using the val() function on a select element can occasionally result in a null value

Having an issue with my select. For some reason, when I use $(".test").val(), it sporadically returns null. Can't seem to figure out what's causing this inconsistency. console.log($('.test').val()); <script src="https://ajax.googl ...

Unraveling the Enigma of Event Handlers: Mastering the Organization of a Sprawling jQuery File within an AJAX

I've created a web application that heavily relies on AJAX and jQuery for DOM manipulation and making AJAX requests. However, I'm facing a problem with my JavaScript file: The issue is that my JavaScript file consists of a huge collection of eve ...

Using Python and Selenium to retrieve information from the attribute 'data-label'

https://i.stack.imgur.com/zb7f5.jpg try: details = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "DataTables_Table_5")) ) scores_list = details.find_elements_by_tag_name('tbody') for sco ...

Execute AJAX request for two individual buttons

I have a scenario where I want to use two buttons to open separate PHP pages, but I would like to trigger both buttons with a single function. The AJAX function should then determine which button was clicked and open the corresponding PHP page - for exam ...

What steps should I take to display a Material UI grid in React without triggering the warning about the missing unique key

In my current project, I am utilizing React and Material UI to display a grid. The grid's rows are populated from an array of data, with each row containing specific information in columns structured like this: <Grid container> {myData.map((re ...

Achieving Equal Spacing Between Containers without Using Grid or Flexbox

I'm struggling to evenly space four child containers within a single parent container without using Flex/Grid, as I haven't covered those topics yet. I tried setting each child container to 25% width of the parent and centering them with text-ali ...

Swapping images when hovering over a list item or anchor tag

I recently came across an intriguing issue with changing the img src on a:hover. Check out this SCREENSHOT When hovering over the <a> sector, it's not showing the white image as expected. I have black and white icons, and I want the img src to ...

How can we effectively map Typescript Enums using their keys without relying on a Map?

Consider the following Enum instances: export enum TopicCategories { GUIDES = 'Guides', TASKS = 'Tasks', CONCEPTS = 'Concepts', FORMULAS = 'Formulas', BLOGS = 'Blogs' } export enum Top ...

Prevent ESLint from linting files with non-standard extensions

My .estintrc.yaml: parser: "@typescript-eslint/parser" parserOptions: sourceType: module project: tsconfig.json tsconfigRootDir: ./ env: es6: true browser: true node: true mocha: true plugins: - "@typescript-eslint" D ...

The presence of an additional bracket is being triggered by the JSON array following

Currently, I am developing a function in Freemarker to iterate through all selected checkboxes and then display the same response on the form itself. The form is structured in Freemarker format, and I am utilizing JavaScript to create separate arrays for e ...

Changing the active state of an icon when clicked in a list generated by v-for in Vue

In my Vue.js application, I have created a list of objects where each object represents a star. I want to implement a feature where a star changes color when it is clicked. However, the issue I am facing is that when I click on one star, all the stars in ...