{"id":3943,"date":"2026-11-26T15:12:35","date_gmt":"2026-11-26T07:12:35","guid":{"rendered":"http:\/\/www.lire-en-serie.com\/blog\/?p=3943"},"modified":"2026-11-26T15:12:35","modified_gmt":"2026-11-26T07:12:35","slug":"how-to-do-two-way-binding-in-rivets-4daa-39a55d","status":"publish","type":"post","link":"http:\/\/www.lire-en-serie.com\/blog\/2026\/11\/26\/how-to-do-two-way-binding-in-rivets-4daa-39a55d\/","title":{"rendered":"How to do two-way binding in Rivets?"},"content":{"rendered":"<p>Hey there, <a href=\"https:\/\/www.zeyaohardware.com\/other-fasteners\/rivets\/\">Rivets<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.zeyaohardware.com\/uploads\/43263\/small\/12-point-screw-for-automotive-parts4d750.jpg\"><\/p>\n<p>If you\u2019ve spent any time working with lightweight front-end frameworks, you\u2019ve probably run into Rivets.js. It\u2019s that no-fuss, data-binding library that lets you connect your UI to JavaScript objects without the bloat of larger frameworks\u2014perfect for small projects, internal tools, or static sites that need a little dynamic oomph. But I\u2019ve noticed one question keeps popping up in dev forums, customer Slack threads, and even casual coffee chats with the teams we work with: \u201cHow do I actually get two-way binding working in Rivets?\u201d<\/p>\n<p>As a Rivets.js supplier, I spend a lot of time walking through this exact scenario with our clients, and the truth is, it\u2019s not as straightforward as it is in Vue or React\u2014those 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\u2019s flexible, not prescriptive, so you have to build that two-way connection yourself, but that\u2019s where the power lies.<\/p>\n<p>Let me break this down the way I do with every new client. First, let\u2019s make sure we\u2019re 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\u2014just 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.<\/p>\n<h3>Starting With the Basics: Rivets Core Setup<\/h3>\n<p>Before we dive into two-way binding, let\u2019s confirm you\u2019ve got the basics right. A lot of folks skip this step and run into issues later. First, you include Rivets in your project. We\u2019ve worked with teams that add it via npm, CDN, or even just a script tag in their HTML\u2014all 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.<\/p>\n<p>Here\u2019s a super simple starter: let\u2019s say you have a text input in your HTML, and you want to tie it to a <code>user.name<\/code> property in your JavaScript. The standard one-way binding for text would be something like <code>&lt;input type=&quot;text&quot; rv-text=&quot;user.name&quot;&gt;<\/code>. That works for displaying the model value in the input, but if the user types something, <code>user.name<\/code> won\u2019t update. That\u2019s where the gap is\u2014and that\u2019s where two-way binding comes in.<\/p>\n<h3>The Secret Sauce: The <code>rv-value<\/code> Modifier (or, Wait, Is It <code>rv-two-way<\/code>?)<\/h3>\n<p>I used to get this wrong all the time. Early on, I thought Rivets had a built-in <code>rv-two-way<\/code> directive, similar to Vue\u2019s <code>v-model<\/code>, but it doesn\u2019t\u2014at least not out of the box. Instead, Rivets uses directives that are designed to be flexible, and for form elements, the key is the <code>rv-value<\/code> directive paired with an event handler that updates the model when the input changes.<\/p>\n<p>Wait, let\u2019s clarify that. Let\u2019s take that same input example. If you want the model to update when the user types, you need to listen for the <code>input<\/code> event (not just <code>change<\/code>, which fires when the input loses focus) and update <code>user.name<\/code> every time that event runs. In Rivets, you can hook into events with the <code>rv-on<\/code> directive. So putting that together: <code>&lt;input type=&quot;text&quot; rv-value=&quot;user.name&quot; rv-on-input=&quot;user.name = event.target.value&quot;&gt;<\/code>.<\/p>\n<p>Let me pause here. If you\u2019re new to Rivets, that line might look a little weird. <code>rv-value<\/code> is one-way: it takes the model value and puts it in the input. <code>rv-on-input<\/code> is listening for the browser\u2019s <code>input<\/code> event (which fires on every keystroke, unlike <code>change<\/code>), and when it does, it sets <code>user.name<\/code> to whatever the user typed into the input. That\u2019s two-way binding, right there, in three lines.<\/p>\n<p>But wait\u2014what about other form elements? It\u2019s not just text inputs. Checkboxes, dropdowns, radio buttons, textareas\u2014each needs a slightly different approach for the <code>rv-on<\/code> event, because their values work differently. Let\u2019s walk through a few common ones because I\u2019ve seen so many teams mess this up by trying to copy-paste the text input code into a checkbox and it breaks.<\/p>\n<h4>Checkboxes: A Common Pitfall<\/h4>\n<p>Checkboxes don\u2019t have a <code>value<\/code> property\u2014they have a <code>checked<\/code> property. So if you try to use <code>rv-value<\/code> for a checkbox, it won\u2019t work. Instead, you need to bind the <code>checked<\/code> property with <code>rv-checked<\/code> (the one-way part) and then use <code>rv-on-change<\/code> (since checkboxes fire <code>change<\/code> when their state toggles, not <code>input<\/code>) to update the model.<\/p>\n<p>Here\u2019s the correct checkbox two-way binding: <code>&lt;input type=&quot;checkbox&quot; rv-checked=&quot;user.rememberMe&quot; rv-on-change=&quot;user.rememberMe = event.target.checked&quot;&gt;<\/code>. That makes sense. When the checkbox is checked, <code>user.rememberMe<\/code> becomes <code>true<\/code>; when it\u2019s unchecked, it becomes <code>false<\/code>. If you set <code>user.rememberMe<\/code> to <code>true<\/code> in your JS, the checkbox automatically checks itself\u2014full two-way sync.<\/p>\n<p>I once had a client who tried to use <code>rv-value<\/code> 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\u2019t abstract those element-specific differences. That\u2019s the tradeoff, but once you learn the element-specific patterns, it\u2019s way easier to troubleshoot.<\/p>\n<h4>Dropdowns (Select Elements)<\/h4>\n<p>Select elements are another case where <code>rv-value<\/code> works, but only for single-select dropdowns. For multi-select, you have to adjust because the <code>value<\/code> property is an array of selected options. Let\u2019s do single-select first, which is the most common: <code>&lt;select rv-value=&quot;user.country&quot; rv-on-change=&quot;user.country = event.target.value&quot;&gt;&lt;option value=&quot;&quot;&gt;Select a country&lt;\/option&gt;&lt;option value=&quot;us&quot;&gt;United States&lt;\/option&gt;&lt;option value=&quot;ca&quot;&gt;Canada&lt;\/option&gt;&lt;\/select&gt;<\/code>. That works: when the user picks a country, <code>user.country<\/code> updates; if <code>user.country<\/code> is set to <code>&quot;ca&quot;<\/code>, the dropdown automatically selects Canada.<\/p>\n<p>For multi-select, though, you need to grab all selected options and map their values into an array. The <code>change<\/code> event still works, but the event target\u2019s <code>selectedOptions<\/code> is a collection, so your event handler has to turn that into an array. The Rivets binding would look like this: <code>&lt;select multiple rv-value=&quot;user.favoriteColors&quot; rv-on-change=&quot;user.favoriteColors = Array.from(event.target.selectedOptions, option =&gt; option.value)&quot;&gt;<\/code>. That\u2019s a tiny bit more code, but it\u2019s still straightforward once you know how multi-select values work.<\/p>\n<h3>Making It Reusable: Why Our Clients Love Our Pre-Built Two-Way Bindings<\/h3>\n<p>Here\u2019s the thing: all that element-specific code works great for small projects, but when you\u2019re building a full app, writing <code>rv-on-input<\/code> or <code>rv-on-change<\/code> for every single form field gets tedious fast. That\u2019s why as a Rivets.js supplier, we\u2019ve built custom utilities that wrap that logic up so you don\u2019t 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.<\/p>\n<p>Our two-way binding utilities for Rivets let you swap that long input line for something simpler, like <code>&lt;input type=&quot;text&quot; rv-model=&quot;user.name&quot;&gt;<\/code> and <code>&lt;input type=&quot;checkbox&quot; rv-model=&quot;user.rememberMe&quot;&gt;<\/code>. Under the hood, we\u2019ve mapped each form element to its correct event and property: text inputs use <code>input<\/code> events and <code>value<\/code>, checkboxes use <code>change<\/code> and <code>checked<\/code>, dropdowns use <code>change<\/code> and <code>value<\/code>, 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\u2019t match a pattern), the UI updates to show an error right away.<\/p>\n<p>A lot of devs ask me, \u201cWhy not just use Vue\u2019s <code>v-model<\/code>?\u201d And the answer is simple: Rivets is lighter, and it doesn\u2019t 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.<\/p>\n<h3>Common Mistakes to Avoid<\/h3>\n<p>After working with dozens of teams, I\u2019ve 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\u2019t have to learn them the hard way.<\/p>\n<p>First, don\u2019t use <code>rv-on-change<\/code> for text inputs. The <code>change<\/code> event only fires when the input loses focus, so if you want real-time binding (like a live preview as the user types), <code>rv-on-input<\/code> is the right choice. I once had a client build a search feature with <code>rv-on-change<\/code> and wondered why it only updated after the user clicked away from the search bar. That\u2019s a simple fix, but it\u2019s a costly one if you don\u2019t know to check the event type.<\/p>\n<p>Second, don\u2019t forget to initialize your model. Rivets won\u2019t throw an error if your model property doesn\u2019t exist, but if it\u2019s <code>undefined<\/code> instead of an empty string, boolean, or array, some bindings might behave weirdly. For example, if <code>user.name<\/code> is <code>undefined<\/code> and you use <code>&lt;input rv-value=&quot;user.name&quot;&gt;<\/code>, the input will be empty, but if you set it later, it won\u2019t populate correctly. Always start your model with default values: <code>const user = { name: '', rememberMe: false, country: '' }<\/code>.<\/p>\n<p>Third, don\u2019t try to bind to non-form elements with two-way logic. Rivets works best for form elements and simple text display\u2014if you\u2019re trying to do two-way binding on a <code>&lt;div&gt;<\/code> or <code>&lt;span&gt;<\/code>, you\u2019re overcomplicating it. For those, just use one-way binding with <code>rv-text<\/code> or <code>rv-html<\/code> and update the model programmatically when you need to change the display.<\/p>\n<h3>When to Go Custom (And When to Use Our Tools)<\/h3>\n<p>Let\u2019s talk about use cases. If you\u2019re 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\u2019s only a few lines, and you don\u2019t need extra tools. But if you\u2019re 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.<\/p>\n<p>We also work with teams that need custom two-way binding for unique elements\u2014like date pickers, file inputs, or custom UI components. Rivets lets you extend its directives, so we can build a custom <code>rv-date<\/code> directive that handles the <code>change<\/code> event for a date picker and syncs the model correctly, no matter which date picker library you\u2019re using. That\u2019s the flexibility that makes Rivets stand out, and as a supplier, we tailor that to your specific stack.<\/p>\n<h3>Wrapping It Up<\/h3>\n<p>Two-way binding in Rivets isn\u2019t as instant as it is in bigger frameworks, but that\u2019s 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\u2019t want. Whether you\u2019re 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.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.zeyaohardware.com\/uploads\/43263\/small\/rack-mounting-heat-sink-screw57843.jpg\"><\/p>\n<p>If you\u2019re working on a project with Rivets and you\u2019re tired of writing repetitive two-way binding code, or you need custom bindings for your unique UI elements, we\u2019re here to help. We\u2019ve 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\u2019 two-way binding capabilities.<\/p>\n<p><a href=\"https:\/\/www.zeyaohardware.com\/bolt\/other-bolts\/\">Other Bolts<\/a> References<\/p>\n<ol>\n<li>Rivets.js Official Documentation.<\/li>\n<li>Front-End Frameworks: A Comparative Analysis for Lightweight Projects.<\/li>\n<li>Custom Directives in Rivets.js: Best Practices for Form Binding.<\/li>\n<\/ol>\n<hr>\n<p><a href=\"https:\/\/www.zeyaohardware.com\/\">Huizhou Zeyao Hardware Products Co., Ltd.<\/a><br \/>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.<br \/>Address: 1\/F, Building A, Huifeng West Third Road No. 108, Chenjiang St., Zhongkai High-Tech District,Huizhou, Guangdong, China<br \/>E-mail: sales@hky-hardware.com<br \/>WebSite: <a href=\"https:\/\/www.zeyaohardware.com\/\">https:\/\/www.zeyaohardware.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, Rivets If you\u2019ve spent any time working with lightweight front-end frameworks, you\u2019ve probably run &hellip; <a title=\"How to do two-way binding in Rivets?\" class=\"hm-read-more\" href=\"http:\/\/www.lire-en-serie.com\/blog\/2026\/11\/26\/how-to-do-two-way-binding-in-rivets-4daa-39a55d\/\"><span class=\"screen-reader-text\">How to do two-way binding in Rivets?<\/span>Read more<\/a><\/p>\n","protected":false},"author":15,"featured_media":3943,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3906],"class_list":["post-3943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-rivets-4112-39d2e6"],"_links":{"self":[{"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/posts\/3943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/comments?post=3943"}],"version-history":[{"count":0,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/posts\/3943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/posts\/3943"}],"wp:attachment":[{"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/media?parent=3943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/categories?post=3943"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lire-en-serie.com\/blog\/wp-json\/wp\/v2\/tags?post=3943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}