Brownman Revival
Brownman Revival

Reputation: 3850

Remove entire column of table dynamic and n number of columns jquery

if (target >= 4) {
        $('.floor').slice(target).parent().parent().remove();
        //since I select the .floor input box, I have to use the parent() function two times, to move the selector up to the <tr> element
    } else {
        $('.floor').slice(4).parent().parent().remove();
        //since I select the .floor input box, I have to use the parent() function two times, to move the selector up to the <tr> element
    }

This is how I delete n number of row depending on a number in a text field which will then be the value of target. How can I make the same concept except that I need to delete entire column?

Update

<table id="flooring">
    <tr>
        <td><strong><p>Flooring</p></strong>
        </td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>
            <hr/>
        </td>
        <td>
            <span>1st Floor</span>

        </td>
        <td>
            <span>2nd Floor</span>

        </td>
        <td>
            <span>3rd Floor</span>

        </td>
        <td>
            <span>4th Floor</span>

        </td>
    </tr>
    <tr>
        <td><span>Reinforced Concrete</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="loor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>

    <tr>
        <td><span>Plain Cement</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Marble</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Wood</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>
    <tr>
        <td><span>Tiles</span>
        </td>
        <td>

            <input type="checkbox" class="floor1st checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor2nd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor3rd checkboxfloor" name="flooring[]" />

        </td>
        <td>

            <input type="checkbox" class="floor4th checkboxfloor" name="flooring[]" />

        </td>
    </tr>

</table>

This is the default html and I also add column here depending on input text. When the previous input is bigger than the current that is the time I want to delete entire column that is what this is for.

Upvotes: 2

Views: 1593

Answers (2)

Brownman Revival
Brownman Revival

Reputation: 3850

if (target >= 4) {
        $("table#.floor tr").find('td:eq('+target+')').nextAll().remove();
    } else {
        $("table#.floor tr").find('td:nth-child(5)').nextAll().remove();
    }

I ended up using this code. Hopefully someone will be helped by this small code. The code above will choose all td with the index of target and all td next to it

Upvotes: 0

Barmar
Barmar

Reputation: 780974

Use an :nth-child selector:

$("#tableid").find("tr :nth-child("+target+")").remove();

To remove all columns starting from target:

var col;
while ((col = $("#tableid").find("tr :nth-child("+target+")")).length) {
    col.remove();
}

Upvotes: 1

Related Questions