The constraints that keep jQuery in place
On a legacy site I help maintain, jQuery is the front-end's foundation and it is not in good shape. Two constraints rule out escape: a full rewrite is underway, so large refactors are not getting approved, and new dependencies to the existing system require a full security review that has historically taken up to a year. jQuery is therefore the only JavaScript library available, simply because it is already there.
The front end was built by developers with little awareness of best practices, so quality is uneven and unidiomatic — quintessential jQuery spaghetti. Working in it at scale is instructive rather than pitiable; there is always more legacy code in the world than greenfield work.
Diagnosing spaghetti: the missing .toggle()
jQuery's real value is smoothing over cross-browser compatibility while making DOM queries and mutations easy. The trouble is that direct DOM manipulation does not scale if you are not careful. The more of it you write, the more defensive against DOM mutation you become, and with weak scope management the result is an app whose entire state lives in the DOM. You can never trust the state an element will be in when you need to change it, and updates can arrive from anywhere.
Code tends toward the procedural as a consequence: it bloats with explicit instructions that pull data out of the DOM and force it into the needed shape. That is why a missing .toggle() is a reliable smell — without certainty about visibility, you fall back to .show() and .hide(). Those methods are not inherently harmful, but they suggest bigger problems.
A reactive pattern borrowed from better tools
The remedy my coworkers and I have found takes its cue from the reactive frameworks we would rather use: observables and state management. Hand-rolled state objects plus event-driven update functions, with the DOM treated as a one-way dataflow template, produce more predictable results that are easier to change over time. Each of us implements this differently; my version is flavored like a Vue drop-in and leans on some "advanced" CSS.
Two pieces make up the script: a State object holding every value on the page, and a group of events.
var State = {
num: 0,
firstName: "",
lastName: "",
titleColor: "black",
updateState: function(key, value){
this[key] = value;
$("[data-text]").each(function(index, elem){
var tag = $(elem).attr("data-tag");
$(elem).text(State[tag]);
});
$("[data-color]").each(function(index, elem){
var tag = $(elem).attr("data-tag");
$(elem).attr("data-color", State[tag]);
});
}
};
Wiring markup with data- attributes
I lean heavily on custom HTML attributes, which sidestep a long-standing annoyance: classes doing double duty as CSS and JavaScript hooks, which makes a script brittle. Separating the two, classes go back to being classes, and attributes carry whatever metadata or styling hook is needed.
Every element that displays data carries a data-tag attribute whose value matches a property on State, plus a valueless attribute naming the transformation to apply. This example uses two: text and color.
<h1 data-color>jDux is super cool!</h1>
Events feed the state
All data changes originate from events. Each event of interest gets its own .on() method and triggers an update method with two arguments: the State property to change and its new value.
$("#inc").on("click", function(){
State.updateState("num", State.num + 1)
});
$("#dec").on("click", function(){
State.updateState("num", State.num - 1)
});
$("#firstNameInput").on("input", function(){
State.updateState("firstName", $(this).val() )
});
$("#lastNameInput").on("input", function(){
State.updateState("lastName", $(this).val() )
});
$('[class^=button]').on("click", function(e) {
State.updateState('titleColor', e.target.innerText);
});
One update function, whole page refreshed
State.updateState() is what keeps the page in sync. On each run it applies the incoming property and then the two transformations. For text, it collects all data-text nodes and sets their text from the tagged property. For color, it sets the element's data-color attribute from the tagged property and lets CSS style data-color to show the right appearance. Re-rendering everything on every change is not efficient, but it is far simpler — an imperfect solution suited to an imperfect codebase.
$(document).ready(function(){
State.updateState();
});
A document.ready handler runs the update function on load to display defaults. Those defaults can come from the DOM, from an AJAX call, or by pre-populating the State object, as in this example.
Why it holds up
The pattern's payoff is a single source of truth: state that you control, trust and can enforce. If confidence in the DOM is lost, call the update function with no arguments and the page becomes consistent with the state object again.
It is hokey and primitive, and no one should build an entire system this way. Where better tools are available, use them. But in a highly restrictive legacy codebase, writing the next feature with Reactive jQuery can make the code — and the work — simpler.



