2

I am trying to use each function, but I need to match the exact attribute name

jquery

$('[data*="name"]').each(function(i, obj){dostuff}

I need to {dostuff} only when name is found and not when names is found.Any suggestions ?

Josh
  • 180
  • 1
  • 10

5 Answers5

3

remove the *

$('[data="name"]').each(function(i, obj){dostuff}

according to the Documentation the * matches also substrings, so just remove it if you don't need :)

pumpkinzzz
  • 2,907
  • 2
  • 18
  • 32
2

I think you should do it in that way.

$('[data="name"]').each(function(i, obj){ dostuff });

I hope I have helped you! :)

1

You should be able to just use the attribute equals selector, which is basically identical to your current selector sans the * (as *= is used as a "contains" selector) :

// This would perform your operation to every element that had a data attribute of
// "name" (i.e. <span data="name">, etc.)
$('[data="name"]').each(function(i, obj){ 
      dostuff(); 
});
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
1

Just

 $('[data="name"]').each(function(i, obj){dostuff}

OR

 $('[data*="name"]:not([data*=names])').each(function(i, obj){dostuff}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
1

Remove the "contains" condition from the attribute selector (the * character)

$('[data="name"]').each(function(i, obj){dostuff}

Using the * after the attribute name

Selects elements that have the specified attribute with a value containing a given substring

Read here

Dark Hippo
  • 1,255
  • 2
  • 15
  • 35