0

I'm using this variable to get one or more values matching to what I need.

var mail = $("#dat").contents().find("td:contains('" + name + "')" ).siblings("td:nth-child(2)").map(function() { return $(this).text(); }).get();

My data look like this:

<tr><td> Name Second-Name </td><td>Email</td></tr>
<tr><td> Name Second-Name </td><td>Email 2</td></tr>
<tr><td> Name Second-Name Third-Name </td><td>Email 3</td></tr>

When I use Name Second-Name as name value, this variable returns Email, Email 2 and Email 3, even with Third-Name inside the last table row. What I need is a variable that returns Email and Email 2 only, ignoring the last row - where I have another name.

How can I do that? Thanks!

Zeca Novaes
  • 379
  • 1
  • 2
  • 17
  • So basically `.find("td:contains('" + name + "'):not(:contains('" + other_name + "'))" )` – adeneo Mar 02 '16 at 22:59
  • Are you looking for an [exact match](http://stackoverflow.com/questions/15364298/make-jquerys-contains-select-only-exact-string) instead of a contains? – Stryner Mar 02 '16 at 23:00
  • Thanks! No, adeneo, my data is more complicated than that, I don't have all `other_names` to use on my variable – Zeca Novaes Mar 02 '16 at 23:12

1 Answers1

1

Try this:

var name = " Name Second-Name ";
var mail = $('td').filter(function(index) { return $(this).text() === name }).siblings("td:nth-child(2)").map(function() { return $(this).text(); }).get();

Fiddle: http://jsfiddle.net/ksL6opd7

Fabius
  • 506
  • 9
  • 22