0
jQuery('table tr td').each(function() {
    console.log ( this );
});

In the above example, console.log ( this ); prints:

<td valign="top">
    Color: Blue<br>
    Size: M<br>
    <div>Qty: 2</div>
    <div>Price: $14.95</div>
</td>

How can I get the current selector, which is "table tr td," from "this"?

I tried using console.log ( jQuery(this).selector ); but it prints nothing.

GTS Joe
  • 3,612
  • 12
  • 52
  • 94
  • You can do `var t = $(this);` and then manipulate t as a jquery object. – Tyler Roper Oct 12 '16 at 20:00
  • 1
    Why do you want to get the selector? – gen_Eric Oct 12 '16 at 20:02
  • How would I get the selector "table tr td" from $(this)? – GTS Joe Oct 12 '16 at 20:02
  • Rocket, because I need to pass it as a parameter to another function. – GTS Joe Oct 12 '16 at 20:03
  • I misunderstood, however I believe `.selector` was removed in JQuery 1.9. See [here](http://stackoverflow.com/questions/9382028/get-the-current-jquery-selector-string) for some more info. Why can you not hard-code the selector when you pass it though? You're not dynamically generating it before hand, and if you *were*, you could just re-use that same declaration. – Tyler Roper Oct 12 '16 at 20:04
  • @GTSJoe: You *already* know what it is. Why not just save it in a variable *before* the `.each`? Something like: `var selector = 'table tr td'; jQuery(selector).each(function(){ console.log(selector); });`. – gen_Eric Oct 12 '16 at 20:05
  • 1
    I don't know what your second function is doing, but it should be enough to use `$(this)` as a function argument – empiric Oct 12 '16 at 20:05
  • @empiric He wants to be able to return `table tr td` from within his `each`, dynamically. You used to be able to use `.selector` but this was deprecated and removed in 1.7 and 1.9, respectively. – Tyler Roper Oct 12 '16 at 20:06
  • 2
    This seems like an XY problem. Show how you're trying to use the selector, and we can probably show how to do it with the supported jQuery API. – Barmar Oct 12 '16 at 20:09

2 Answers2

0

You can put a different class attribute to each <td> for example.

$('table tr td').each(function() {
    alert($(this).attr("class"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="t1">Color: Blue<br>
    Size: M<br>
    <div>Qty: 2</div>
    <div>Price: $14.95</div></td>
    
  </tr>
  <tr>
    <td class="t2">Color: Red<br>
    Size: L<br>
    <div>Qty: 3</div>
    <div>Price: $15.99</div></td>
  </tr>
  <tr>
    <td class="t3">Color: Yellow<br>
    Size: M<br>
    <div>Qty: 2</div>
    <div>Price: $14.95</div></td>
  </tr>
  <tr>
    <td class="t4">Color: LightBlue<br>
    Size: M<br>
    <div>Qty: 4</div>
    <div>Price: $12.95</div></td>
  </tr>
</table>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
0

Then try the following

var i = 1;
$('table tr td').each(function() {
 $(this).addClass("t" + i);
    alert($(this).attr("class"));
 i++;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
  <tr>
    <td>Color: Blue<br>
    Size: M<br>
    <div>Qty: 2</div>
    <div>Price: $14.95</div></td>
    
  </tr>
  <tr>
    <td>Color: Red<br>
    Size: L<br>
    <div>Qty: 3</div>
    <div>Price: $15.99</div></td>
  </tr>
  <tr>
    <td>Color: Yellow<br>
    Size: M<br>
    <div>Qty: 2</div>
    <div>Price: $14.95</div></td>
  </tr>
  <tr>
    <td>Color: LightBlue<br>
    Size: M<br>
    <div>Qty: 4</div>
    <div>Price: $12.95</div></td>
  </tr>
</table>
Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47