14

I'm trying to target attributes names which contains a certain word & not in the kind of way you're thinking.

Lets say I have:

<div data-foo-bar="hello"></div>

How can I target elements which have 'data-foo' in the attribute name?

daryl
  • 14,307
  • 21
  • 67
  • 92

6 Answers6

12

I don't think you can target attribute names the same way you target attribute values. You can, however, use .filter() to do this somewhat efficiently:

$('div').filter(function() {
  for (var property in $(this).data()) {
    if (property.indexOf('fooBar') == 0) {
      return true;
    }
  }

  return false;
});​

Notice that data-foo-bar has been converted to fooBar.

Demo: http://jsfiddle.net/Tyj49/3/

Blender
  • 289,723
  • 53
  • 439
  • 496
  • 3
    I'm well-acquainted with jQuery. Those selectors match the *value* of a given attribute, not the attribute name. – Blender Mar 03 '12 at 05:45
  • This one seems better. And if you want to just cache the filtered objects for later, you could do it like this: http://jsfiddle.net/Tyj49/4/ While I'm going to guess that reimagining the approach is probably better in the long run (hard to say without knowing the use case), this gets a vote for sure. – Greg Pettit Mar 03 '12 at 05:46
  • Why didn't you check hasOwnProperty here ? – Royi Namir Sep 21 '14 at 09:38
4

I'm not sure you can do that. I've had a peek at the API and you only seem to be able to do deeper partial matching on the value, not the attribute name itself.

I'm going to give you the potentially frustrating advice of, "don't do this". ;-) In essence, what you're after is a bunch of divs that match a more general category. Not just data-foo-bars, but ALL data-foos! Why not just give all data-foos a classname that you can select from, or a separate attribute like data-foo="true"?

In other words, rather than sticking strictly to the question as asked, I'd be curious to know what the use case is so that maybe a lateral move in thinking can solve your problem just as effectively. Many ways to skin a cat and all that.

Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
  • Well, my alternative would have been to have 2 data attributes. But I figured if there's a will there's a way, so stackoverflow came to mind. :) – daryl Mar 03 '12 at 05:40
1

This is just an alternative solution. If you can ensure the attribute's name you're looking for is not present in the element text, what you could do is getting the outerHtml value of the element and then make a substring search:

$("elementsToCheck").each(function () {
    var elemHtml = $('<div>').append($(this).clone()).html(); //clone to get the outerHTML
    if (elemHtml.indexOf("attributeName") >= 0) {
        // current element contains the attribute with name "attributeName"
    }
});

Hope this helps.

Edgardo
  • 29
  • 4
0

Use .data() to get all the data attributes of an element, and then use .filter to get the elements that have data-validation* attributes.

var validated_elements = $("p").filter(function() {
    var found = false;
    $.each($(this).data(function(key) {
        if (key.match(/^data-validation/)) {
            found = true;
            return false;
        }
    }
    return found;
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try this

$().ready(function() {  
  var test=$('#div_test').data("foo-bar");
  alert(test);
});

HTML:

<div data-foo-bar="hello" id="div_test"></div>
Greg Pettit
  • 10,749
  • 5
  • 53
  • 72
pkachhia
  • 1,876
  • 1
  • 19
  • 30
  • By golly it seems to work. Might be worth putting in a few counter-example tests to see if it's fragile or not, but here it is in action: http://jsfiddle.net/YkTJy/ – Greg Pettit Mar 03 '12 at 05:33
  • Crazy. I almost downvoted until I saw your comment @GregPettit. That will teach me for jumping to conclusions. – daryl Mar 03 '12 at 05:39
  • 1
    @Brogrammer: Wait, your question seems to say that you want to match the partial `data-foo`, not the full `data-foo-bar`. Is that not correct? –  Mar 03 '12 at 05:40
  • 1
    Hrm, I don't think it's working much after all. It will return the first value of a foo-bar but it can't return them all and won't let you get an array of jQuery objects with foo-bar. So it's not going to meet the OP's needs. – Greg Pettit Mar 03 '12 at 05:40
  • 1
    Yeah, partial. Not sure how it slipped by me that this test wasn't partial. Probably has something to do with it being nearly 6am. Bah. – daryl Mar 03 '12 at 05:47
-7

Try something like this

var element = $('div[data-foo-bar=hello]')​;
anantkpal
  • 100
  • 4
  • not sure why this got stomped on with votedowns, seems like simplest valid concept so far http://jsfiddle.net/wHuV2/ – charlietfl Mar 03 '12 at 05:41
  • The OP is trying to match the attribute *name*, not the actual value. The question was somewhat ambiguous. – Blender Mar 03 '12 at 05:42
  • understood about value but the attribute is far simpler than 4 line filters – charlietfl Mar 03 '12 at 05:43
  • 3
    I dunno about 'stomped on'. It doesn't provide a solution to the question, so it's a downvote for me. In fact, it compounds the problem because now it's selecting not only data-foo-bars, but data-foo-bars with the value "hello". ;-) – Greg Pettit Mar 03 '12 at 05:50
  • You always get your reputation back, when you remove downvoted answer. – vp_arth Mar 31 '15 at 11:57