Solace Solace - 6 months ago 9
jQuery Question

What is the second argument in this jquery select statement?

I have seen it here. What is meant by

tbl
in the following statement? What does it imply?

var rows = $('tr', tbl);

Answer

The tbl in the above is another dom element. This is passed in as a context (jQuery( selector [, context ] )) for the selector 'tr'. http://api.jquery.com/jquery/#jQuery1

So essentially this:

$('tr', tbl);

says return me everything that matches the selector 'tr' in the element(s) tbl.


Example

So given

<table>
  <tr>first</tr>
<table>
<table id="test">
   <tr>second</tr>
</table>

This returns varying results:

//context is global
$('tr') => first & second

//restrict the context to just the second table 
//by finding it and passing it into the selector
var tbl = $('#test');
$('tr', tbl) => just second