1

I want to hide HTML Table row which has 0.00 value in 3rd column. I tried using JQuery and CSS but it doesn't work.

Here is my code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link href="globalCSS.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">

<script>
$('tr').each(function()
{
    var tr = $(this);
    if (tr.find('td:eq(2)').text()=="0.00") 
        tr.addClass('hidden');
});
</script>

<style>
.hidden
{
    display: none
}
</style>

</head>
<body>

<div>
<table border=1>
    <tbody>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td> 0.00 </td>
        </tr>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td> 1.00 </td>
        </tr>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td> 0.00 </td>
        </tr>
    </tbody>
</table>
</div>

</body>
</html>

Sorry for putting all the things in single jsp file. But I am new with JQuery. Can u anyone please suggest me something?

3 Answers 3

1

Try this. First, put your scripts at the end of the document (It's also the best practice). Trim the <td> text (your example had spaces in td values.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link href="globalCSS.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">     

<style>
.hidden
{
    display: none
}
</style>

</head>
<body>

<div>
<table border=1>
    <tbody>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td>0.00</td>
        </tr>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td> 1.00 </td>
        </tr>
        <tr>
            <td> 1 </td>
            <td> 2 </td>
            <td> 0.00 </td>
        </tr>
    </tbody>
</table>
</div>
<script>
$('tr').each(function()
{
    var tr = $(this);   
    if (tr.find('td:eq(2)').text().trim()=="0.00") 
    {
        tr.addClass('hidden');
}
});
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, Please Check now
It doesn't work but thank you for giving me idea of .trim(). up vote is for .trim()
0
$('tr').each(function () {
  a = $(this).children('td:eq(2)').text();
  if (a == "0.00") {
      $(this).addClass('hidden');
  }
})

demo - http://jsfiddle.net/cc1wefkz/

1 Comment

Can you please tell me? If I have put this in <head> it doesn't work but if I put this after <div> it works properly. Can you please tell me the reason behind this?
0
$('#tableId tbody tr td:nth-child(2)').each( function(){
   var tr_value = $(this).text();
   if(tr_value == '0.00'){
     $(this).addClass('hidden');
   }       
});

1 Comment

$(this) is running through the .each of td elements so you'd need to address the parent of the td in order to reach the tr to hide it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.