javascript - Select elements which start with "data-" -
how can select plan javascript or jquery every element has attribute starts "data-"?
i've tried
$("[data-*"])
but doesn't work.
here non-jquery function need:
function getalldataelements() { //get dom elements var elements = document.getelementsbytagname("*"); //array store matches var matches = []; //loop each element (var = 0; < elements.length; i++) { var element = elements[i]; //get attributes element , loop them var attributes = element.attributes; (var j = 0; j < attributes.length; j++) { //get name of attribute var attr = attributes.item(j).nodename; //check if attibute name starts "data-" if (attr.indexof("data-") == 0) { matches.push(element); //add matches } } } return matches; //return results }
which can used so:
var results = getalldataelements(); results.foreach(function (i) { i.style.color = "#ff0000"; });
Comments
Post a Comment