AnsweredAssumed Answered

Access FeatureLayer label innerHTML in JS API 4.0

Question asked by jungf on Nov 11, 2016


I wonder if there is possibility to access and change the innerHTML of labels on FeatureLayer.

The approach described here: does not work because LableLayer is not in 4.0.

Code snippet was:

labelsLayerNode = labelsLayerNode || labelsLayer.getNode();
            if (!labelsLayerNode) {
            // find text labels in the labelsLayer svg element
            var textLabels = dojoQuery('text', labelsLayerNode);
            // cycle through each label and add a new row on the dollar sign.
            // TODO: ignore empty strings so they don't cause unnecessary newlines.
            arrayUtil.forEach(textLabels, function(lbl) {
                var fieldArr = lbl.innerHTML.split('$');
                // only format these labels if there's more than one field here
                if (fieldArr.length > 1) {
                    lbl.innerHTML = fieldArr[0];
                    for (var i = 1; i < fieldArr.length; i++) {
                        // create a tspan for each subsequent field
                        var tspan = document.createElementNS('', 'tspan');
                        tspan.setAttributeNS(null, 'x', lbl.getAttributeNS(null, 'x'));
                        tspan.setAttributeNS(null, 'dy', '1.5em');
                        tspan.innerHTML = fieldArr[i];

The task is to split the label to multiple lines and set different colors for each line (in 4.0 there is no problem to do the line spliting:

labelExpressionInfo: {

but what about the different colors?


Thanks for help.