PricingDocsAcademy
Bluesky ...
Wed, Dec 11, 11:03 PM

Get width of element

  • Patrick Mast

    2 months ago

    I have this global function:
    function elementWidth (args, ctx) {

    const element = document.getElementById(args.element);
    const elementWidth = Math.round(parseFloat(window.getComputedStyle(element).width));

    return elementWidth;

    }


    How can I convert it to a Toddle formula?

    I want the function to be encapsulated within the component, making it easily portable between projects without the need to remember to add a separate global function.

    Or.. Is it also possible to add this function inside a "Script" tag in the component?
  • Jacob Kofoed

    2 months ago

    I'm not entirely sure what you mean with portable between projects, but if you want to share a formula between projects, then I suggest creating a package. Then you can install it where needed, and maintain it in a single place. And other people can benefit from it as well ๐Ÿ™Œ

    Also, to get the width of an element, I suggest you use getBoundingClientRect().width rather than getComputedStyle as the computed style doesn't actually tell you the real width on the screen. It won't always take into account transforms and paddings. Also, you won't have to parse the value as it is already numerical.
    ๐Ÿ’ก1

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code โ€” but accessible to your entire team!

Try toddle โ€” it's free!

ยฉ Copyright 2024 toddle. All rights reserved.