LostCore
2/5/2015 - 12:56 PM

Get an HTML tag attribute

Get an HTML tag attribute

/**
 * Get an HTML tag attribute
 * @param string the HTML tag
 * @param attr the attribute to retrieve
 * @returns {string}
 */
function getAttr(string, attr) {
    "use strict";
    attr = new RegExp(attr + '=\"([^\"]+)\"', 'g').exec(string);
    return attr ?  window.decodeURIComponent(attr[1]) : '';
}

//Usage:
var id = getAttr("<div id='foo'>bar</div>","id");