Retrieve a collection of rows from a table by referencing a specific cell value

Is there a way to retrieve a list of rows from a table where one cell has a value of 0.00 without using a loop in jQuery?

Preferably, with code that does not involve looping (avoiding the use of the each statement).

<table id="products">
<tr><td>Name</td><td>Product</td><td>Price</td></tr>
<tr id="1"><td>gdfgdf</td><td>1</td><td aria-describedby="Price">0.00</td></tr>
<tr id="2"><td>gdf455g</td><td>2</td><td aria-describedby="Price">0.00</td></tr>
<tr id="3"><td>gdf43gdf</td><td>1</td><td aria-describedby="Price">6.50</td></tr>
<tr id="4"><td>gdf44g</td><td>2</td><td aria-describedby="Price">7.00</td></tr>
</table>

I am looking to extract the first two rows from the table where the price is 0.00.

Answer №1

Alright, here's the solution for you:

$("#products td").filter(function () {
                        return $(this).text() == 0;
                   }).closest("tr").hide();

Using this code will hide the rows where at least one cell is equal to 0.

Cheers and happy coding!

Answer №2

Here's a method that achieves the desired outcome without visibly using a loop function like each. However, it still involves looping through all the rows in the background to check for the specified text.

$('#products tr').filter(function(){
    return $(this).find('td:last').text() == '0.00';
}).doSomething();

Check out the DEMO here

Answer №3

Using .each method for highlighting cells with value of 0


$('td').each(function() {
    if ($(this).text() == 0) {
        $(this).css("background", "yellow");
    }
});

Updated version using :contains to highlight first two rows instead of just cells


var elements = $('td:contains(0)');
for (i=0; i<element.length; i++) {
    if (element[i].innerHTML == 0)
        $(element[i]).parent().css("background", "yellow");
}

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

Ways to change the background color of a Bootstrap row excluding column padding

I'm working on adjusting the background to fit within bootstrap's column sizes while still extending across the entire row. Does anyone have suggestions on how to achieve this? Here is what I've tried: .yourcontent { margin-bottom: 5px ...

Viewing the JSON value within a select tag using Zend Framework 2

My approach involves fetching data from a database: public function getAllProjectDomain() { $dbadapter = $this->adapter; $sql = new Sql($dbadapter); $select = $sql->select(); $select->columns(array('cmain&ap ...

How to stylishly showcase a list in a horizontal row using HTML and CSS`

Is there a way to modify this code so that the list is displayed in a horizontal line menu? Currently, the code displays the list vertically but I want it to be displayed in a horizontal line. How can I achieve this? <html><head><style> ...

Decrease the padding value inherited from CSS while reducing the numerical value

One of the challenges I am facing is with a class that is added to different sections of a website to indicate that they are editable. This class is called .editable. Currently, when a user is logged in and hovers over an editable section, a red dashed bo ...

Fetching the duplicated data from a submitted form using serialize() method

When submitting form inputs using the serialize() function in jQuery and retrieving the data in PHP through a POST request, I encountered an issue where only the multi-select dropdown data was getting duplicated. The rest of the input data was coming in co ...

The design breaks occur exclusively in the Android default browser

I am experiencing issues with the design of a nested div element when viewed in the default Android browser. Unfortunately, I don't have access to tools that would allow me to identify the cause of this problem. However, the design functions correctly ...

connecting the content management system page to a specific image

I recently designed a CMS page named collection.html and included the following code: {{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="7" template="catalog/product/list.phtml"}} Now, I am looking ...

Design a table within an angularjs directive that incorporates a unique rowspan feature

After creating a custom directive to display tabular data, I encountered an issue with the code. Here is my current implementation: app.directive('searchResult', function () { return { restrict: 'E', scope: { conten ...

Observing the HTMLInputElement Object in Action with JavaScript

After utilizing JavaScript to extract the value from the SPAN element, placing it into a hidden form field, and submitting the data, I am puzzled by the unexpected result I am receiving. Can you help me understand why this is happening? <form onsubmit= ...

The constant frustration of trying to extract a predefined AJAX return value, only to

My Ajax call and PHP function are causing an issue in Firefox. Despite receiving an HTTP 200 status, the response shows the value "false," but it is not being passed to the success part of the Ajax handler - instead, I always get undefined. I have plenty ...

Ways to adjust the dimensions of a textarea based on character count

When attempting to utilize the cols and rows attributes of the <textarea> element in order to set the width and height in characters, I have encountered issues even without implementing CSS. Take a look at this example: link I have configured a 5x5 ...

Guide to sending an ajax request from one page and receiving the response on a different page

Looking for advice on sending Ajax requests using jQuery and HTML5. I have multiple pages within my application. Is it possible to send an Ajax request from one page (e.g sync.html) and receive a response on another page (e.g home.html)? I am aware of alte ...

What are the steps to achieve the desired PrimeFaces theme appearance for selectOneMenu?

Need help with a JSF Primefaces project using the omega theme. The selectOneMenu dropdowns are not displaying correctly (missing line). Current look: https://i.stack.imgur.com/vF4ms.png Expected look: https://i.stack.imgur.com/hXsIB.png Any suggestion ...

Using JavaScript to dynamically load Cascading Style Sheets based on the current

I am trying to dynamically load different CSS files based on the current date (season). I attempted to tweak an image script that I found on Stack Overflow, but it didn't yield the desired result. Could someone please guide me on where I might be ma ...

The selection discrepancy is due to the misalignment between the cursor and white space in the CSS styling

Upon close inspection, I've discovered that a discrepancy exists between the selection on my webpage and the cursor position. After investigating further, I uncovered the following reasons: Presence of white-space between the start tag <p> and ...

Modifying the class of multiple images within an array

I am excited to share my first post here and express my gratitude for all the solutions I have found on this platform. I encountered an issue with using .removeClass and .addClass in my recent program. I am loading multiple pictures into an array called F ...

Using jQuery and AJAX to dynamically pass a variable into the URL parameter in dynamic HTML pages

By utilizing jQuery, I am dynamically updating the content of a div with a form that consists of two textboxes and a button. Upon clicking this button, ajax is employed to retrieve results from a public webservice based on the data gathered from the textbo ...

ES6 import of CSS file results in string output instead of object

Too long; Didn't read I'm facing an issue where the css file I import into a typescript module resolves to a string instead of an object. Can someone help me understand why this is happening? For Instance // preview.ts import test from './ ...

Use setTimeout and setInterval with the initial input without parentheses or double quotation marks

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var count=0; function increaseCount(){ document.getElementById('txt').value=count; count++; setTimeout(increaseCount(),1000); } </script> </head&g ...

Can you explain the alternative prev function within ReactJs?

Is there a ReactJs alternative to the prev function in jQuery? Despite extensive research, I have been unable to find a solution. I have tried using previousElementSibling,previousSibling,nextElementSibling, and nextSibling without success. Can anyone off ...