Hedu911
Hedu911

Reputation: 133

How to select a element in the clicked table row of a HTML table using Jquery?

I have a table with 2 columns, I'm using PHP to generate dynamically rows to the table. Every table row have the following fields:

What I like to do is to hide the div containing the two inputs type text when the checkbox is checked, I tried to get the parent of the clicked checkbox using Jquery and then hide the corresponding div inside the currrent <tr> but it's not working.

This is the HTML code to see how is my table generated:

<table>

<tr>
    <th>Hide?</th>
    <th>Inputs</th>
</tr>

<tr>
    <td>
        <input type="checkbox" name="checkBoxes[]">
    </td>
    <td>
        <div class="two-inputs">
            <input type="text" name="inputs[]">
            <input type="text" name="inputs[]">
        </div>
    </td>

</tr>

<tr>
    <td>
        <input type="checkbox" name="checkBoxes[]">
    </td>
    <td>
        <div class="two-inputs">
            <input type="text" name="inputs[]">
            <input type="text" name="inputs[]">
        </div>
    </td>

</tr>

Upvotes: 0

Views: 672

Answers (2)

Lowkase
Lowkase

Reputation: 5699

$('input:checkbox').click(function() {
    if (!$(this).is(':checked')) {
        $(this).parent().next('.two-inputs').hide();
    }
});

Upvotes: 0

Anoop
Anoop

Reputation: 23208

Try This. jsfiddle

$("input:checkbox").click(function(){
   if(this.checked){
      $(this).parent().next().find("div").hide();
   }  
    else{
             $(this).parent().next().find("div").show();
    }

});

Upvotes: 1

Related Questions