Spygol Spygol - 1 year ago 121
Javascript Question

Set onclick for childnodes jquery or javascript

Hello I want to set a onclick for every table row in jquery, but i have no idea where to begin and I suck at searching so I was hoping to get some help here.
I've tried this

var tableRows = $("tableID").children();

But the console.log(tableRows) only returns Prev object r.fn.init[0], so I guess I don't have anything to work with from that. How can i get the tableRows in an array and run through them with a for loop?

Answer Source

Try this:

$('document').ready(function() {
  var tableRows = $("#tableID td");
  tableRows.on('click', function(e) {
    console.log('%s %s %s', e.target.tagName, e.target.innerHTML, "clicked");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="tableID">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download