# Adding text dynamically to custom elements

**URL:** <https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109>\
**Category:** Modeler\
**Created:** [April 16, 2021, 3:15pm UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109 "2021-04-16T15:15:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinaG](https://forum.bpmn.io/user_avatar/forum.bpmn.io/martinag/32/2677_2.png) [@martinaG](https://forum.bpmn.io/u/martinaG)\
**Post date:** [April 16, 2021, 3:15pm UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/1 "2021-04-16T15:15:29Z")

</div>

Hi,  
my goal is to add text to show Tasks and DataStoreReference elements names .

The relevant code

```auto
const task = elementFactory.createShape({ type: 'bpmn:Task' });
var task_name = 'task1';
  
modeling.createShape(task, { x: 100, y: 100, width: 100, height: 80 },
    modeler_canvas.getRootElement()
);
var textAnnotation = elementFactory.createShape({
  type: 'bpmn:TextAnnotation',
  businessObject: moddle.create('bpmn:TextAnnotation', {
    text: task_name
   })
});
modeling.appendShape(task, textAnnotation, {
  x: 100,
  y: 100 }
);

```

and I get  
 ![task_name_a](https://forum.bpmn.io/uploads/default/original/2X/5/549824314624e53dd902bf8bc329a0180a4b475f.jpeg)

where the red text is what I’m trying to achieve.  
Thank you for your help.

---

<div class="post-metadata">

**Author:** ![Niklas\_Kiefer](https://forum.bpmn.io/user_avatar/forum.bpmn.io/niklas_kiefer/32/3882_2.png) [@Niklas\_Kiefer](https://forum.bpmn.io/u/Niklas_Kiefer)\
**Post date:** [April 19, 2021, 7:03am UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/2 "2021-04-19T07:03:31Z")

</div>

So you want to render the label of the text annotations [in a different position](https://github.com/bpmn-io/bpmn-js/blob/develop/lib/draw/BpmnRenderer.js#L1626)? Why that?

---

<div class="post-metadata">

**Author:** ![martinaG](https://forum.bpmn.io/user_avatar/forum.bpmn.io/martinag/32/2677_2.png) [@martinaG](https://forum.bpmn.io/u/martinaG)\
**Post date:** [April 19, 2021, 9:09am UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/3 "2021-04-19T09:09:53Z")

</div>

I’d need the `task_name` to be displayed in the middle of the shape `Task`. Maybe this is not handled by `bpmn:TextAnnotation`…

---

<div class="post-metadata">

**Author:** ![Niklas\_Kiefer](https://forum.bpmn.io/user_avatar/forum.bpmn.io/niklas_kiefer/32/3882_2.png) [@Niklas\_Kiefer](https://forum.bpmn.io/u/Niklas_Kiefer)\
**Post date:** [April 19, 2021, 9:40am UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/4 "2021-04-19T09:40:11Z")

</div>

Sorry, then I misunderstood you. [Here is the part of the task renderer](https://github.com/bpmn-io/bpmn-js/blob/develop/lib/draw/BpmnRenderer.js#L871). As you can see, the label of a task is already rendered in the middle. What do you expect then?

---

<div class="post-metadata">

**Author:** ![martinaG](https://forum.bpmn.io/user_avatar/forum.bpmn.io/martinag/32/2677_2.png) [@martinaG](https://forum.bpmn.io/u/martinaG)\
**Post date:** [April 19, 2021, 10:06am UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/5 "2021-04-19T10:06:41Z")

</div>

Thank you, now I know TextAnnotation is not what I’m looking for. I’ve seen the [renderEmbeddedLabel()](https://github.com/bpmn-io/bpmn-js/blob/d4078f1ab0541a259632f11449c992a312a7ccd5/lib/draw/BpmnRenderer.js#L871) and [renderLabel()](https://github.com/bpmn-io/bpmn-js/blob/d4078f1ab0541a259632f11449c992a312a7ccd5/lib/draw/BpmnRenderer.js#L449) functions. My goal is to set `semantic.name` to the value of a local variable, a variable that is set automatically without the need for the user to click on the shape and enter the text.  
I create the Task shape as follow:

```auto
const task = elementFactory.createShape({ type: 'bpmn:Task' });
var task_name = 'task1';
modeling.createShape(task, { x: x_task, y: current_Y, width: task_width, height: task_height },
   modeler_canvas.getRootElement()
);

```

How can I use the `renderEmbeddedLabel()` on the `task` element created with the code above?  
Thank you in advance for your help

---

<div class="post-metadata">

**Author:** ![Niklas\_Kiefer](https://forum.bpmn.io/user_avatar/forum.bpmn.io/niklas_kiefer/32/3882_2.png) [@Niklas\_Kiefer](https://forum.bpmn.io/u/Niklas_Kiefer)\
**Post date:** [April 19, 2021, 3:02pm UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/6 "2021-04-19T15:02:09Z")

</div>

The renderer is using an _element’s label_ to render it. We have API for that to update the label whenever you want: [`Modeling#updateLabel`](https://github.com/bpmn-io/bpmn-js/blob/develop/lib/features/modeling/Modeling.js#L63).

```auto
const modeling = modeler.get('modeling');

const element = modeling.createShape(...); // as you are already doing

// update the label of an element
modeling.updateLabel(element, 'new Label');

```

---

<div class="post-metadata">

**Author:** ![nikku](https://forum.bpmn.io/user_avatar/forum.bpmn.io/nikku/32/5130_2.png) [@nikku](https://forum.bpmn.io/u/nikku)\
**Post date:** [July 22, 2025, 3:38pm UTC](https://forum.bpmn.io/t/adding-text-dynamically-to-custom-elements/6109/7 "2025-07-22T15:38:06Z")

</div>

Closed on July 22, 2025, 3:38pm UTC


