Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of colspan

I have tried different jQuery approaches:

var num = $(this).attr('colspan').text();
var num = $(this).attr('colspan').val();
var num = $(this).('td[colspan]').val();
var num = $(this).('td[colspan]').text();
var num = $(this).('td[colspan]').attr('id');

and this from W3Schools: var num = document.getElementById($(this)).colSpan;

no luck. It's either undefined or an uncaught TypeError in my console.log()


EDIT: I have tried your suggestions and it still isn't working. Here is part of the code in context:

$('table td').on('mouseenter mouseleave', function(e){
    var th, index, id, td;
    if(e.type === 'mouseenter'){
        id = $(this).attr('id');
        td = $(this);
        var num = $(this).attr('colspan');
        console.log("td = "+td+" colspan = "+num);
        td.addClass('highlight');

var $table_body_scroll=$('table.body_scroll'),
            header_table=$( '<table aria-hidden="true" class="header_table"><thead><tr><td></td></tr></thead></table>' ),
            scroll_div='<div class="body_scroll"></div>';
        var $targetHeaderTable=$("table.header_table").eq(index);

        $('thead tr th').each(function() {
            console.log("th = "+$(this).text());
            console.log("id = "+id);
            if ($(this).text() == id)
            {
                var num = td.attr('colspan');
                //console.log("td = "+td+" colspan = "+num);
                $(this).addClass('highlight');
            }
        });

        index = $('td').index($(e.target));
        $(e.target).prevAll().addClass('highlight');

    }else{
        $('.highlight').removeClass('highlight');
    }
});

and my console log: td = [object Object] colspan = undefined

like image 567
Burning Hippo Avatar asked Mar 21 '23 15:03

Burning Hippo


1 Answers

Use prop to get the value set for the attribute/property, this will reflect any changes made to the property with JS after the page load:

$(this).prop("colSpan");

JS Fiddle: http://jsfiddle.net/9u8L2/1

like image 91
Kevin Bowersox Avatar answered Mar 28 '23 13:03

Kevin Bowersox