Skip to content Skip to sidebar Skip to footer

How To Get All Html Attributes Which Start With Something (the Attribute Names, *not* Their Values!)

I would like to get all the elements/nodes in an HTML page which contain attributes that start with something (again, the attribute names start with something, not their values!).

Solution 1:

here's a simple demo to find all elements that contain an attribute starting with mce_. might need some refinements.

functiongetMCE() {
    var el, attr, i, j, arr = [],
        reg = newRegExp('^mce_', 'i'),                //case insensitive mce_ pattern
        els = document.body.getElementsByTagName('*'); //get all tags in bodyfor (i = 0; i < els.length; i++) {                 //loop through all tags
        el = els[i]                                    //our current element
        attr = el.attributes;                          //its attributesdance: for (j = 0; j < attr.length; j++) {     //loop through all attributesif (reg.test(attr[j].name)) {              //if an attribute starts with mce_
                arr.push(el);                          //push to collectionbreak dance;                           //break this loop
            }
        }
    }
    return arr;
}

console.log(getMCE())​

Solution 2:

Try this:

FUNCTIONS

//custom selector expression
$.extend($.expr[':'],{
attr:function(o,i,m){
  var attrs=$.getAttrAll(o),re=m[3],found=false;
  $.each(attrs,function(k,v){
  if(newRegExp(re).test(v)) { return found=true;}
});
return found;
} 
});
// get all atrributes of an element
$.getAttrAll=function(el){
  var rect = [];
  for (var i=0, attrs=el.attributes, len=attrs.length; i<len; i++){
    rect.push(attrs.item(i).nodeName);
  }
  return rect;
};

` USAGE

// calling custom selector expression :attr(regexp)
$(function(){
  $('body').find(':attr("^mce_")').css({background:'yellow'});
});

HTML

<body><pmce_style="height:50px"id="x"data-hello="hello">selected</p><divnot_mce_bogus="abc">not_mce_bogus</div><divmce_href="http://rahenrangan.com">selected</div><p>othrs</p></body>

Solution 3:

One option, if you don't mind temporarily altering your DOM, is to extract your HTML into a string and search for the attributes via RegExp. When you find the attributes, you could append a "needle" in the DOM so that you can use jQuery to select the elements.

Here is a working concept (run with console open):

http://jsfiddle.net/skylar/N43Bm/

Code:

$.fn.extend({

    findAttributes: function(attribute) {

        var attributeFinder = newRegExp(attribute + '(.+)="', "gi");
        var elementHTML = this.html().replace(attributeFinder, "data-needle='pin' "+attribute+"$1=\"");

        this.html(elementHTML);

        returnthis.find("[data-needle=pin]").removeAttr('data-needle');
    }

});

console.log($("body").findAttributes('mce_'));

Note: my regexp is not great. You'll have to take better care than I have in this example.

Solution 4:

Try this: (I tried putting * instead of a tag but it colored all the elements including those who do not have mce_style attribute as well)

a[mce_style] { color : red; }​

Demo : http://jsfiddle.net/Tcdmb/

More info : https://developer.mozilla.org/en/CSS/Attribute_selectors

Post a Comment for "How To Get All Html Attributes Which Start With Something (the Attribute Names, *not* Their Values!)"