Posted in

How to do two-way binding in Rivets?

Hey there, Rivets

If you’ve spent any time working with lightweight front-end frameworks, you’ve probably run into Rivets.js. It’s that no-fuss, data-binding library that lets you connect your UI to JavaScript objects without the bloat of larger frameworks—perfect for small projects, internal tools, or static sites that need a little dynamic oomph. But I’ve noticed one question keeps popping up in dev forums, customer Slack threads, and even casual coffee chats with the teams we work with: “How do I actually get two-way binding working in Rivets?”

As a Rivets.js supplier, I spend a lot of time walking through this exact scenario with our clients, and the truth is, it’s not as straightforward as it is in Vue or React—those frameworks handle two-way binding (syncing data between the model and UI elements) almost out of the box. Rivets is a bit more intentional, and that intentionality is why so many teams stick with it. It’s flexible, not prescriptive, so you have to build that two-way connection yourself, but that’s where the power lies.

Let me break this down the way I do with every new client. First, let’s make sure we’re on the same page: two-way binding means if a user types in an input field, the underlying JavaScript object (your model) updates instantly, and if the model changes programmatically, the UI updates right back. No manual event listeners, no extra sync code—just a two-way street between your data and what the user sees. With Rivets, that street is there, but you have to lay the pavement.

Starting With the Basics: Rivets Core Setup

Before we dive into two-way binding, let’s confirm you’ve got the basics right. A lot of folks skip this step and run into issues later. First, you include Rivets in your project. We’ve worked with teams that add it via npm, CDN, or even just a script tag in their HTML—all work, but we recommend npm for most ongoing projects because it makes updates easier. Once Rivets is loaded, you need to bind it to your model and your DOM.

Here’s a super simple starter: let’s say you have a text input in your HTML, and you want to tie it to a user.name property in your JavaScript. The standard one-way binding for text would be something like <input type="text" rv-text="user.name">. That works for displaying the model value in the input, but if the user types something, user.name won’t update. That’s where the gap is—and that’s where two-way binding comes in.

The Secret Sauce: The rv-value Modifier (or, Wait, Is It rv-two-way?)

I used to get this wrong all the time. Early on, I thought Rivets had a built-in rv-two-way directive, similar to Vue’s v-model, but it doesn’t—at least not out of the box. Instead, Rivets uses directives that are designed to be flexible, and for form elements, the key is the rv-value directive paired with an event handler that updates the model when the input changes.

Wait, let’s clarify that. Let’s take that same input example. If you want the model to update when the user types, you need to listen for the input event (not just change, which fires when the input loses focus) and update user.name every time that event runs. In Rivets, you can hook into events with the rv-on directive. So putting that together: <input type="text" rv-value="user.name" rv-on-input="user.name = event.target.value">.

Let me pause here. If you’re new to Rivets, that line might look a little weird. rv-value is one-way: it takes the model value and puts it in the input. rv-on-input is listening for the browser’s input event (which fires on every keystroke, unlike change), and when it does, it sets user.name to whatever the user typed into the input. That’s two-way binding, right there, in three lines.

But wait—what about other form elements? It’s not just text inputs. Checkboxes, dropdowns, radio buttons, textareas—each needs a slightly different approach for the rv-on event, because their values work differently. Let’s walk through a few common ones because I’ve seen so many teams mess this up by trying to copy-paste the text input code into a checkbox and it breaks.

Checkboxes: A Common Pitfall

Checkboxes don’t have a value property—they have a checked property. So if you try to use rv-value for a checkbox, it won’t work. Instead, you need to bind the checked property with rv-checked (the one-way part) and then use rv-on-change (since checkboxes fire change when their state toggles, not input) to update the model.

Here’s the correct checkbox two-way binding: <input type="checkbox" rv-checked="user.rememberMe" rv-on-change="user.rememberMe = event.target.checked">. That makes sense. When the checkbox is checked, user.rememberMe becomes true; when it’s unchecked, it becomes false. If you set user.rememberMe to true in your JS, the checkbox automatically checks itself—full two-way sync.

I once had a client who tried to use rv-value on a checkbox and spent three hours wondering why their model never updated. They were so used to other frameworks that they forgot Rivets doesn’t abstract those element-specific differences. That’s the tradeoff, but once you learn the element-specific patterns, it’s way easier to troubleshoot.

Dropdowns (Select Elements)

Select elements are another case where rv-value works, but only for single-select dropdowns. For multi-select, you have to adjust because the value property is an array of selected options. Let’s do single-select first, which is the most common: <select rv-value="user.country" rv-on-change="user.country = event.target.value"><option value="">Select a country</option><option value="us">United States</option><option value="ca">Canada</option></select>. That works: when the user picks a country, user.country updates; if user.country is set to "ca", the dropdown automatically selects Canada.

For multi-select, though, you need to grab all selected options and map their values into an array. The change event still works, but the event target’s selectedOptions is a collection, so your event handler has to turn that into an array. The Rivets binding would look like this: <select multiple rv-value="user.favoriteColors" rv-on-change="user.favoriteColors = Array.from(event.target.selectedOptions, option => option.value)">. That’s a tiny bit more code, but it’s still straightforward once you know how multi-select values work.

Making It Reusable: Why Our Clients Love Our Pre-Built Two-Way Bindings

Here’s the thing: all that element-specific code works great for small projects, but when you’re building a full app, writing rv-on-input or rv-on-change for every single form field gets tedious fast. That’s why as a Rivets.js supplier, we’ve built custom utilities that wrap that logic up so you don’t have to write it every time. Our clients tell us that cutting out that repetitive code saves them hours of work, especially on internal tools where forms are everywhere.

Our two-way binding utilities for Rivets let you swap that long input line for something simpler, like <input type="text" rv-model="user.name"> and <input type="checkbox" rv-model="user.rememberMe">. Under the hood, we’ve mapped each form element to its correct event and property: text inputs use input events and value, checkboxes use change and checked, dropdowns use change and value, and multi-selects handle that array logic automatically. We even add basic validation hooks so if your model value is invalid (like an email that doesn’t match a pattern), the UI updates to show an error right away.

A lot of devs ask me, “Why not just use Vue’s v-model?” And the answer is simple: Rivets is lighter, and it doesn’t force you into a full framework. We have clients that use Rivets alongside jQuery, or in parts of a larger app built with React, where they just need a small bit of dynamic form functionality. Our pre-built two-way bindings keep that Rivets code clean without making them switch frameworks.

Common Mistakes to Avoid

After working with dozens of teams, I’ve seen a few mistakes pop up again and again when setting up two-way binding in Rivets. Let me run through the big ones so you don’t have to learn them the hard way.

First, don’t use rv-on-change for text inputs. The change event only fires when the input loses focus, so if you want real-time binding (like a live preview as the user types), rv-on-input is the right choice. I once had a client build a search feature with rv-on-change and wondered why it only updated after the user clicked away from the search bar. That’s a simple fix, but it’s a costly one if you don’t know to check the event type.

Second, don’t forget to initialize your model. Rivets won’t throw an error if your model property doesn’t exist, but if it’s undefined instead of an empty string, boolean, or array, some bindings might behave weirdly. For example, if user.name is undefined and you use <input rv-value="user.name">, the input will be empty, but if you set it later, it won’t populate correctly. Always start your model with default values: const user = { name: '', rememberMe: false, country: '' }.

Third, don’t try to bind to non-form elements with two-way logic. Rivets works best for form elements and simple text display—if you’re trying to do two-way binding on a <div> or <span>, you’re overcomplicating it. For those, just use one-way binding with rv-text or rv-html and update the model programmatically when you need to change the display.

When to Go Custom (And When to Use Our Tools)

Let’s talk about use cases. If you’re building a small tool, a one-off form, or just adding a few dynamic bits to a static site, writing the two-way binding code manually is totally fine. It’s only a few lines, and you don’t need extra tools. But if you’re building an app with 10+ forms, or forms with multiple fields, or forms that have complex logic (like dependent fields where selecting a country changes the available states), our pre-built Rivets utilities will save you so much time.

We also work with teams that need custom two-way binding for unique elements—like date pickers, file inputs, or custom UI components. Rivets lets you extend its directives, so we can build a custom rv-date directive that handles the change event for a date picker and syncs the model correctly, no matter which date picker library you’re using. That’s the flexibility that makes Rivets stand out, and as a supplier, we tailor that to your specific stack.

Wrapping It Up

Two-way binding in Rivets isn’t as instant as it is in bigger frameworks, but that’s a feature, not a bug. It teaches you exactly how the UI and model connect, no hidden magic, and it lets you build exactly the bindings you need, no extra code you don’t want. Whether you’re writing the basic code yourself for a small project or looking for pre-built tools to streamline your work, Rivets gives you the control without the bloat.

If you’re working on a project with Rivets and you’re tired of writing repetitive two-way binding code, or you need custom bindings for your unique UI elements, we’re here to help. We’ve supported teams of all sizes, from small startups to enterprise companies, with Rivets solutions that fit their exact needs. Reach out to start a conversation about how we can help you make the most of Rivets’ two-way binding capabilities.

Other Bolts References

  1. Rivets.js Official Documentation.
  2. Front-End Frameworks: A Comparative Analysis for Lightweight Projects.
  3. Custom Directives in Rivets.js: Best Practices for Form Binding.

Huizhou Zeyao Hardware Products Co., Ltd.
Huizhou Zeyao Hardware Products Co., Ltd. is one of the most professional rivets manufacturers and suppliers in China, specialized in providing high quality products and service. We warmly welcome you to wholesale customized rivets made in China here from our factory.
Address: 1/F, Building A, Huifeng West Third Road No. 108, Chenjiang St., Zhongkai High-Tech District,Huizhou, Guangdong, China
E-mail: sales@hky-hardware.com
WebSite: https://www.zeyaohardware.com/