1

I have a couple of elements like

<p data-lang-bg="Bulgarian" data-lang-en="English" >Text</p>
<p data-lang-bg="Other bulgarian text" data-lang-en="Other english text" >Text 2</p>

I have a difficulties writing a jQuery selector, that will select all elements, who have data attributes starting with data-lang . Is it possible to write such a jQuery selector

4
  • Sorry, I don't believe this is possible. I could see how it would be useful however, maybe file a report for this with the jQuery team. Commented Feb 25, 2013 at 16:41
  • I don't think you can do that. You'd have to iterate over all elements and their datasets. Commented Feb 25, 2013 at 16:43
  • Really, this is what classes are good for. I would simply add class="lang" to each such element and select that instead. Commented Feb 25, 2013 at 16:52
  • I vote to reopen because this question is much more specific (it's about data, not any attribute) and thus can have better answers. Commented Feb 26, 2013 at 17:23

3 Answers 3

1

Here's the nearest I can propose.

Change your HTML to this structure :

<p data-lang='{"bg":"Bulgarian","en":"English"}' >Text</p>

Then use this selector :

$('[data-lang]')

For example, you can get your languages values as maps :

$('[data-lang]').each(function(){
    var localizations = $(this).data('lang'); // parses the map as JSON
    ... use the object, for example localizations['en']
});

This looks to me like the correct way to use the power of custom data attributes.

Sign up to request clarification or add additional context in comments.

Comments

0

This might help.

http://code.google.com/p/jquery-list-attributes/

(function($) {
      // duck-punching to make attr() return a map
      var _old = $.fn.attr;
      $.fn.attr = function() {
          var a, aLength, attributes,   map;
          if (this[0] && arguments.length === 0) {
                    map = {};
                    attributes = this[0].attributes;
                    aLength = attributes.length;
                    for (a = 0; a < aLength; a++) {
                              map[attributes[a].name.toLowerCase()] = attributes[a].value;
                    }
                    return map;
          } else {
                    return _old.apply(this, arguments);
          }
}
}(jQuery));

Source: http://forum.jquery.com/topic/retrieving-all-attributes-of-a-given-element-and-their-values

Comments

0

Use a custom selector:

$.expr[':']['data-lang'] = function (obj) {
    var data = $(obj).data();
    for (i in data) {
        if (data.hasOwnProperty(i)) {
            if (i.indexOf('lang') === 0) {
                return true;
            }
        }
    }
};

Then select them with $('p:data-lang').

1 Comment

This doesn't work as is. You should use $.expr[':']['data-lang'] = function....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.