blockly

Get Google Blockly Block by Id?


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?


Solution

  • 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