How do I get a hidden td element?

How do I get a hidden td element?

herbyxxxherbyxxx Posts: 1Questions: 1Answers: 0
edited December 2019 in Free community support

HTML:

<table id="table">
    <thead>
        <tr>
            <th>Index</th>
            <th>Some headline</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td data-link="somelink">1</td>
            <td>Some information</td>
        </tr>
    </tbody>
</table>

JavaScript:

var table = $('#table').DataTable({
    'columnDefs': [
        {'targets': [0], 'visible': false}
    ]
});

$(document).on('click', '#table tbody tr', function () {
    let td = table.row(this).data()[0];
    //get data-href attribute???
});

With this I just get the text inside the hidden td (number 1). But I actually like to have to whole td or atleast access the data-link attribute.
How do I achieve this? Thanks

Answers

  • magnus@greatlord.commagnus@greatlord.com Posts: 16Questions: 6Answers: 0
    edited December 2019

    some thing like this

     $(document).on('click', '#table tbody tr', function () {
    
                let td = ''
                let attrdata = '';
    
                for ( i = 0; i < $(this)[0].cells.length; i++ ) {
    
                    try {
                        td = $(this)[0].cells[1];
                        attrdata = $(this)[0].cells[1].className;
    
                    } catch {
    
                        // no attribute className not found what should we do
    
                        td = '';
                        attrdata = ''; 
                    }
                    // check here if you got right td tag
                }
    
    
    
            });
    

    this contain only the row element not td, you need access it by child or by cells
    in this example I show how to access it by cells each attri. it exists attributes,
    $(this)[0].cells[1].attributes that you can scan if the attribute exists or not as well.
    I choice not do that in this example instead I using try and catch.

  • magnus@greatlord.commagnus@greatlord.com Posts: 16Questions: 6Answers: 0

    Now with childnode

    $(document).on('click', '#table_kchr tbody tr', function () {
    
                let td = ''
                let attrdata = '';
    
                for ( i = 0; i < this.childElementCount; i++ ) {
    
                    attrdata = $(this.children[i]).attr('Class');
                    if ( attrdata !== undefined ) {
                        // found attribute Class
                        td = this.children[i];
                        // attrdata contain data already
                    } else {
                        // not found
                    }
                }
    
    
            });
    
This discussion has been closed.