Alpine - Js + Jquery - JavaScript Plugin Integration - A Select2 Example Code With Hugo
Alpine - Js + Jquery - JavaScript Plugin Integration - A Select2 Example Code With Hugo
— Proudly sponsored by —
Interested in Alpine.js?
Subscribe to Alpine.js Weekly. A
free, once–weekly email roundup
of Alpine.js news and articles
useful products by Code with Hugo
Alpine.js +
jQuery/JavaScript Plugin
Integration: a Select2
example
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
One of
roundup ofAlpine.js
the jQuery
news andecosystem’s
articles greatest strength close
is the
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 1/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
wealth
Code with Hugo of drop-in plugins available. Share this
Table of Contents
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 2/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
<option></option>
Reset selectedCity
<option
value="London">London</option>
</select>
Trigger selection of New York
<p>Selected value (bound in
Alpine.js): <code x-
text="selectedCity"></code></p>
<p><button @click="selectedCity =
Resources 1× 0.5× 0.25× Rerun
Here’s the full working code in the “No Script Tag Here” style, ready
to copy paste. If you’re interested in the step by step of how we came
to this, read the next sections.
<div
x-init="() => {
select2 = $($refs.select).select2();
selectedCity = event.target.value;
});
select2.val(value).trigger('change');
Interested});
in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
}"
>
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 3/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
<option></option>
</select>
Reset selectedCity
</button>
</p>
<p>
</button>
</p>
<p>
</button>
</p>
</div>
<option></option>
<option value="London">London</option>
</select>
Reset selectedCity
</button>
</p>
<p>
</button>
</p>
<p>
</button>
</p>
</div>
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 5/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
</select>
</div>
Interested
<div
in Alpine.js? Subscribe
Subscribe to Alpine.js
x-data="{ Weekly.
A free,
selectedCity: once–weekly email
'' }"
roundupx-init="
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 6/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
select2 = $($refs.select).select2();
>
</div>
The issue we’re now facing is that the Select2 box can update
without the Alpine.js state being updated. We’ll fix this in the next
step.
<div
x-init="
// select2 instantiation
selectedCity = event.target.value;
});
"
>
</div>
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
Changes to the Select2 box are now reflected in Alpine.js state as per
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 7/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
the following
Code with Hugo screen capture. Share this
We’ve now
Interested seen how to sync selection data from Select2 to Alpine.js
in Alpine.js?
Subscribe
using to
Subscribe Alpine.js Weekly.
A free,callback)
.on('select2:select', . email
once–weekly
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 8/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
Note
Code with Hugo that when we click the “Reset” and “Trigger” buttons, Select2
Share this
doesn’t update, that’s because Alpine.js state updates don’t get
synced back to Select2.
The final step in this guide is to make sure that changes to Alpine.js
state are reflected/synchronised to Select2’s state.
In the $watch callback, we’ll set the value of the select2 instance
with the .val() method and trigger an update using
.trigger('change') . Put together the callback is (value) =>
select2.val(value).trigger('change') , ie. when selectedCity
changes, take the new value, set it as select2’s value and trigger an
update.
<div
x-init="
Interested in Alpine.js?
Subscribe
Subscribe // select2 instantiation & listener binding
to Alpine.js Weekly.
A free, once–weekly email
$watch('selectedCity', (value) => {
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 9/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
});
>
</div>
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 10/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
Next we’ll recap the steps that we’ve just gone through.
Wrapping up - Summary of an
Alpine.js + JavaScript/jQuery
plugin integration
The steps to integrating a jQuery/JavaScript plugin like Select2 are
as follows:
Code with Hugoplugins where events are namespaced with the name:
pluginShare this
That’s it for this post, you can check out the Alpine.js tag on Code
with Hugo for more in-depth Alpine.js guides.
Clay Banks
Interested in Alpine.js?
Subscribe to Alpine.js Weekly.
A free, once–weekly email roundup of Alpine.js
news and articles
[email protected] Subscribe
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 12/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
#alpinejs
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 13/14
22/09/2021 22:24 Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example · Code with Hugo
#JAVASCRIPT
#ES6
#DATA STRUCTURES
HUGO DI FRANCESCO
Interested in Alpine.js?
Subscribe
Subscribe to Alpine.js Weekly.
A free, once–weekly email
roundup of Alpine.js news and articles close
https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/ 14/14