2

I have a JavaScript function for a button. When the button is clicked, it will display information. I'm trying to check if there are no results in the current set and go to the next set of results.

$("#mybutton").on("click", function() { 
    var $rowsNo = $("#table tbody tr").hide().filter(function () {
        var $checkresults =  $.trim($(this).find("td").eq(0).text()) === "1";

        if ($checkresults  != null){
            return $.trim($(this).find("td").eq(0).text()) === "1"
        } else
            return $.trim($(this).find("td").eq(0).text()) === "2"
    }).show();
});

This solution only seems to still be capturing the first option only and displaying null data. is there a better way to handle this type of scenario?

4
  • 4
    And what's the question? Commented Feb 1, 2018 at 19:36
  • 1
    And the question is....? Commented Feb 1, 2018 at 19:36
  • console.log($checkresults) in between declaration and the if, and make sure it's actually null. Commented Feb 1, 2018 at 19:40
  • this solution always seems to result to the first case which is null never passing through to the second. is there a better way to handle this scenario? Commented Feb 1, 2018 at 19:48

1 Answer 1

2

Here's an example that uses data-* attribute and jQuery's .data() method

var $rows = $("#table tbody tr");

$("[data-showrow]").on("click", function() {

  var showrow = $(this).data("showrow"); // Get the data value: "", "1", "2" etc
  if (!showrow) return $rows.show();     // if "" was clicked, show all and exit.

  $rows.hide().filter(function(){
    return $(this).find("td").eq(0).text() == showrow;
  }).show();
  
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-showrow="">All</button>
<button data-showrow="1">1</button>
<button data-showrow="2">2</button>
<button data-showrow="3">3</button>


<table id="table">
  <tbody>
    <tr><td>1</td> <td>Lorem</td></tr>
    <tr><td>2</td> <td>Ipsum</td></tr>
    <tr><td>1</td> <td>Dolor</td></tr>
    <tr><td>1</td> <td>Sit</td></tr>
    <tr><td>2</td> <td>Amet</td></tr>
    <tr><td>3</td> <td>consectetur </td></tr>
    <tr><td>1</td> <td>Adipiscing</td></tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

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.