DEV Community

Rajasegar Chandran
Rajasegar Chandran

Posted on

5 4

Creating a Temperature Converter without a single line of JS

This is the second in the series of posts about using hyperscript a scripting language designed for the web, inspired by HyperTalk, for building components in HTML. If you want to take a look at the first introductory post, you can check it here.

In this post, we are going to build a Temperature Converter component which converts Celcius values to Fahrenheit and vice versa. This component is inspired from the 7GUIs project where we build simple user interfaces in various languages and frameworks to benchmark the complexity and code involved.

So let's just get into the HTML code first and will see what each line of code is doing.

The code

<script src="https://unpkg.com/hyperscript.org@0.0.4"></script>
<p>
  <input id="txtCel" type="number" _="on change set #txtFah.value to  ( 32 + (me.value * (9/5))).toFixed(1) "  value="0"> Celcius = 
  <input id="txtFah" type="number"  _="on change set #txtCel.value to ((5/9) * (me.value - 32)).toFixed(1)" value="32"> Fahrenheit
</p>
Enter fullscreen mode Exit fullscreen mode

As I promised in my first post, all the code examples are terse, having just a few lines to implement simple UI components.

Our first line of code is to import the hyperscript library from the unpkg CDN. The we are creating two input elements with type as number, one for the Celcius values and the other for Fahrenheit.

And then we define the onChange event of each these inputs to update the Celcius and Fahrenheit values deriving from one another based on a simple formula. So when you update the Celcius values, the Fahrenheit values are automatically calculated based on it.

<input id="txtCel" type="number" _="on change set #txtFah.value to  ( 32 + (me.value * (9/5))).toFixed(1) "  value="0">
Enter fullscreen mode Exit fullscreen mode

The script above says

On the 'change' event for this input, set the value of the element with id `txtFah` to the calculated value based on the current element's (me) value and round it off to one decimal point
Enter fullscreen mode Exit fullscreen mode

The on feature allows you to embed event handlers directly in HTML and respond to events with hyperscript:

<button _="on click add .clicked">
  Add The "clicked" Class To Me
</button>
Enter fullscreen mode Exit fullscreen mode

The underscore (_) attribute is where the hyperscript runtime looks for hyperscript on an element by default.

We can access the current element in the event handler using the built-in me object using hyperscript and the other input element using its element identifier. The assignments can be performed using the set command in hyperscript and finally we are rounding off the values to one decimal place using the toFixed function in Javascript.

You can take a look at the component in action in this Codepen.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (2)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Hmm? .toFixed is a disguised JavaScript. Wouldn't it be easier to use onchange or oninput?

Collapse
 
rajasegar profile image
Rajasegar Chandran

Yes it is, but you if you use onchange or oninput you are actually relying completely on JS, the idea here is to do it within HTML itself inside an attribute value. I know it sounds crazy, but it is more expressive grammar using htmx / hyperscript and a shorter way of doing such simple things without relying on JS.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay