Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide HTML Table row using jQuery Variable

I am trying to hide the tr having any td's value is Value1.

Below code is working fine. But, When I use variable instead of value, the code does not work.

I want it to work using variable.

<input type='button' value='hide/show' />
<table id="mytable">
    <thead>
        <tr>
            <th>Col1</th>
            <th>Col2</th>
            <th>Col3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Value1</td>
            <td>Value2</td>
            <td>Value3</td>
        </tr>
        <tr>
            <td>Value4</td>
            <td>Value5</td>
            <td>Value6</td>
        </tr>
    </tbody>
</table>    

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
var this_value = "Value1";
$('input').click(function()
{
    // Working Fine with Value
    $('td:contains("Value1")').parent().toggle();

    // Not Working with Variable
    // $('td:contains(this_value)').parent().toggle();
});
</script>
like image 368
J. Doe Avatar asked Aug 18 '26 09:08

J. Doe


1 Answers

try this

var this_value = "Value1";
$('input').click(function()
{
    $('td:contains('+this_value+')').parent().toggle();
});
like image 68
Parth Shah Avatar answered Aug 20 '26 23:08

Parth Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!