1

I would like to query the DOM to get a block by ID. Currently I have to find all elements by class name, and manually extract the correct index, which is not sustainable.

How can give a block a unique ID?

Thanks

Dazzle
  • 2,880
  • 3
  • 25
  • 52

1 Answers1

9

All blocks already have an id (api docs). However, a Block object isn't a DOM object so you won't find it by using document.getElementById(..). Instead you'll use Workspace.getBlockById(..). Your workspace is usually Blockly.mainWorkspace

Anm
  • 3,291
  • 2
  • 29
  • 40
  • Thanks for that! I can console.log(Blockly.mainWorkspace); but there isn't any getById option? – Dazzle Dec 07 '17 at 17:13
  • Whoops. It looks like `Workspace.getById(..)` is a static method for looking up Workspace objects. The method I meant is `.getBlockById(..)`. Corrected above. – Anm Dec 08 '17 at 00:01