components/jquery
Shim repository packaging jQuery for multiple managers. Install via Composer as components/jquery (also available on Bower, Component, and spm) to include the popular jQuery JavaScript library in your project with standardized versioning and distribution.
Installation
Update to jQuery 4.0.0 via Composer (if using a Laravel-compatible package) or include via CDN in your resources/views/layouts/app.blade.php:
<script src="https://code.jquery.com/jquery-4.0.0.min.js" integrity="sha256-xYDYZ3NcTZHQTw7Q9Mv7mO3Ydclf714+7YhxJ5DhQ=" crossorigin="anonymous"></script>
For Laravel Mix/Webpack, ensure package.json includes:
"jquery": "^4.0.0"
Then run:
npm install jquery@4.0.0
First Use Case Use jQuery 4.0.0 in a Blade view or JavaScript file:
<button id="example-btn">Click Me</button>
<script>
$(document).ready(function() {
$('#example-btn').click(function() {
alert('jQuery 4.0.0 works!');
});
});
</script>
Laravel-Specific Setup
npm install jquery@4.0.0 and update webpack.mix.js:
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', []);
$ is globally available in resources/js/bootstrap.js:
window.$ = window.jQuery = require('jquery');
DOM Manipulation Leverage jQuery 4.0.0's updated methods for dynamic content:
// Toggle visibility (unchanged but optimized)
$('.dynamic-content').toggle();
// AJAX calls with jQuery 4.0.0 (deprecated `ajax` method replaced with `$.ajax`)
$.ajax({
url: '/api/data',
method: 'GET',
success: function(response) {
$('#results').html(response);
}
});
Event Delegation Improved performance with delegated events (jQuery 4.0.0 maintains backward compatibility):
$(document).on('click', '.dynamic-button', function() {
// Handle dynamic elements
});
Laravel Blade Integration Pass jQuery data to Blade templates (unchanged):
// In JS
const userData = @json($user);
Laravel Mix/Webpack
npm install jquery-ui@latest
app.js:
import 'jquery-ui/ui/widgets/draggable';
noConflict() in shared environments (unchanged):
var jq = $.noConflict();
jq(document).ready(function() { ... });
Echo.channel('chat')
.listen('MessageSent', (e) => {
$('#chat').append(`<div>${e.message}</div>`);
});
$('#my-form').submit(function(e) {
e.preventDefault();
$.post('/submit', $(this).serialize(), function(response) {
alert('Success!');
});
});
Global $ Conflicts
$ may conflict with other libraries (e.g., Prototype).jQuery.noConflict() or alias window.jQuery to a custom variable (unchanged).Laravel Mix Version Mismatch
node_modules may differ from CDN.resolve.alias in webpack.mix.js (unchanged):
mix.webpackConfig({
resolve: {
alias: {
jquery: path.resolve(__dirname, 'node_modules/jquery/dist/jquery.js')
}
}
});
CSRF Token in AJAX
csrf_token() helper:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
jQuery in Vue/React
Deprecated Methods in jQuery 4.0.0
.load(), .unload(), and .bind().// Old (deprecated)
$('#element').load('url.html');
// New (jQuery 4.0.0)
$.get('url.html', function(data) {
$('#element').html(data);
});
jQuery 4.0.0 and IE11 Support
Uncaught TypeError: $ is not a function (missing jQuery load).dd($request->all()) in AJAX success handlers to debug payloads.Custom Directives
Create reusable jQuery 4.0.0 snippets in resources/js/directives.js (unchanged):
$.fn.myDirective = function() {
this.on('click', function() { ... });
return this;
};
Laravel Service Providers Bind jQuery to the container (advanced, unchanged):
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('jquery', function() {
return new \Illuminate\Support\Facades\JQuery; // Hypothetical facade
});
}
Testing
Use Laravel’s Http tests with jQuery 4.0.0 (unchanged):
$response = $this->get('/page');
$response->assertSee('<script src="jquery-4.0.0.min.js"></script>');
Performance
<script src="jquery-4.0.0.min.js" defer></script>
```markdown
### Breaking Changes in jQuery 4.0.0
- **Removed Methods**: `.load()`, `.unload()`, `.bind()`, `.unbind()`, `.delegate()`, `.undelegate()`, `.live()`, `.die()`.
- **IE Support**: Dropped support for IE9-11.
- **jQuery 4.0.0 API**: Some internal methods may have changed; consult the [jQuery 4.0.0 Migration Guide](https://jquery.com/upgrade-guide/4.0/) for details.
How can I help you explore Laravel packages today?