Hiding UniqueIds in css class names

I always have a unique id in my databases for each row. When passing this to objects for javascript to manipulate I add it to the class string as ‘id_1’ so I can easily retrieve it later. The GetIds function below returns an array with the id pulled out. It also handles multiple ids in the format of ‘id_name_1’ so we can do something like ‘block id_1 id_Page_1’

function GetIds(classString) {
    //"ashasjas dbid_xxx"
    var ids = classString.split(" ");
    var Result = new Array();
    var i = 0;
    for (i = 0; i < ids.length; i++) {
        if (ids[i].substr(0, 3) == 'id_') {
            var c = String(ids[i]);
            //its starts with id so get the rest
            if (c.indexOf("_") == c.lastIndexOf("_")) {
                //only the number
                Result.push(new keyValue("id", c.substr(c.lastIndexOf("_")+1)));
            }
            else
            {
                //get the command
                var cl = c.length;
                var first = c.indexOf("_") + 1;
                var last = c.lastIndexOf("_");
                Result.push(new keyValue(c.substr(first, last - first), c.substr(last + 1)));
            }
        }
    }
    return Result;
}
function keyValue(key, value) {
    this.key = key;
    this.value = value;
}

Comments are closed.