Welcome to our comprehensive guide on using jQuery with Bootstrap! In this tutorial, we'll cover the basics of both technologies, and demonstrate how to combine them for a powerful web development experience. Let's get started!
jQuery is a popular JavaScript library that simplifies HTML document traversing, event handling, and animating. It allows us to write concise, cross-browser compatible code.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>$(document).ready() function to ensure the DOM is loaded before our scripts run.$(document).ready(function () {
// Your jQuery code here
});Bootstrap is a popular front-end framework that simplifies the process of web development by providing pre-built CSS and JavaScript components.
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>Now that we understand both jQuery and Bootstrap, let's see how we can use them together.
$(document).ready(function () {
// jQuery code that manipulates Bootstrap elements
});<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery with Bootstrap Example</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<!-- Bootstrap Button -->
<button class="btn btn-primary" id="myButton">Click me!</button>
<!-- jQuery and Bootstrap JavaScript -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- jQuery Code -->
<script>
$(document).ready(function () {
$("#myButton").click(function () {
$(this).toggleClass("btn-danger");
});
});
</script>
</body>
</html>In this example, we have a Bootstrap button with an ID of myButton. When the button is clicked, our jQuery code changes its class to btn-danger, which Bootstrap uses to animate the button color change.
Which line of code includes the jQuery library in our HTML file?
That's it for our first jQuery with Bootstrap tutorial! We've covered the basics and seen a practical example. In the next lesson, we'll delve deeper into both technologies and explore more advanced concepts. Happy coding! 💡