Skip to content Skip to sidebar Skip to footer

Fetch Content Of Next Td On Checkbox Click

How to fetch the content of the next td when the check box is clicked

Solution 1:

This seems to work, though it's very HTML-dependant (as is much JavaScript that involves DOM-traversal, of course):

var c = [];

c = window.document.getElementsByTagName('input');

for (var i = 0; i < c.length; i++) {
    if (c[i].type == 'checkbox') {
        c[i].onchange = function() {
            if (this.checked){
                console.log(this.parentNode.nextElementSibling.firstChild.nodeValue.trim());
            }
        };
    }
}​

JS Fiddle demo.

References:

Solution 2:

I have used jQuery. I am guessing that's alright?

    ​$('input:checkbox').live('click', function() {
       alert($(this).parent().next('td').text());
    });​​​​​​​​​​​​​​​

http://jsfiddle.net/6Mwqz/

Solution 3:

Use jQuery, will be better, here your solution:

$('[type=checkbox]').on('click', function(){
  var html;
  if($(this).is(':checked')) {
    html = $(this).parent().next('td').html();
    alert(html);
  }
});
​

DEMO:http://jsfiddle.net/oscarj24/yYuzS/

Post a Comment for "Fetch Content Of Next Td On Checkbox Click"