Josh Josh - 6 months ago 12
Javascript Question

match exact attribute name in jquery

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 ?

Answer

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(); 
});
Comments