Userr
Userr

Reputation: 5

jQuery find checkbox in first td

I have many tables and in that I want to do the following,

  1. find a table which is present in class.
  2. find first tr, first td in a table
  3. check checkbox present first td in a table
  4. if checkbox present in first td then add class.

Below is my code which is not working

function myFunction() {
    debugger;
    var FindClass = $("table.Panel");
    debugger;
    var FindClass = $(".Panel table.Table");
    debugger;
    debugger;
    if (FindClass != null) {
        $("#FindClass tr").find("td:first").tagname("input");

    }
}

Upvotes: 0

Views: 2610

Answers (4)

Userr
Userr

Reputation: 5

We can do this in also this way.

<script type="text/javascript">
function myFunction() {
    debugger;
    var headerRow = $("table.Panel tr:first th:first");
    debugger;
    if (headerRow != null) {
        var checkbox = headerRow.find("input[type=checkbox]");
        if (checkbox[0].type == 'checkbox') {
            headerRow.addClass('checkboxColumns');

            alert('checkbox Found')
        } else {
            alert('not found')
        }
    }

}

</script>

Upvotes: 0

Divakar Gujjala
Divakar Gujjala

Reputation: 803

We can do this in 2 achieve this in 2 simple ways...

    1. Find a table with the class selector. By conditional check we can add the class to the checkbox.
    1. Implementing the complete code in a single line with out performing the conditional operations.

HTML

<table class="Panel">
    <tr>
        <td><input type="checkbox" /></td>
        <td><p>Test</p></td>
    </tr>
    <tr>
        <td>Second TD</td>
    </tr>
</table>

jQuery (1st method)

if($('table.Panel').length > 0) {
    var tblCheckbox = $('table.Panel tr:first td:first input[type=checkbox]');
    if(tblCheckbox.length > 0) {
        tblCheckbox.addClass('clstochkbox');
    }
}

jQuery (1st method)

$('table.Panel tr:first td:first input[type=checkbox]').addClass('clstochkbox');

http://jsfiddle.net/64jv3z6d/

Upvotes: 1

Praveen
Praveen

Reputation: 56509

You can do like this

var chk_box = $("table.Panel tr:first td:first")
                                        .find('input type=["checkbox"]');

if(chk_box.length) {
  $(chk_box.addClass('x')
}

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59262

Check for .length property as jQuery objects are never null. And name it different.

var panelTable = $(".Panel table.Table");
if (panelTable.length) {
   // panelTable has elements
}

Upvotes: 0

Related Questions