I’ve been trying to create a button that shows some input fields when clicked using jQuery (latest version 3.7.1) and it works just fine. However, when I add the tags
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
to include Bootstrap 5 (used later to implement a navigation bar), it stops working and when I click the button nothing happens.
Here is the code I used:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery and Bootstrap 5</title>
<!-- including jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<!-- including Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<!-- jQuery code to make button work -->
<script>
$(document).ready(function(){
$("#button").click(function() {
$("#fn").show();
$("#ln").show();
});
});
</script>
</head>
<body>
<input id="button" type="button" value="Click">
<br>
<div id="fn" hidden>First Name :
<input type="text" />
</div>
<br>
<div id="ln" hidden>Last Name :
<input type="text" />
</div>
</body>
Is there any way I can make the jQuery code work with Bootstrap?
If this is not possible, can anyone suggest a way to implement the same functionality using Bootstrap only?
Thank you!
2
Answers
as can be seen in the reference
you can use removeAttr() method, this worked for me
Rather than stop using
.show()
/.hide()
you can update these so that they add/remove thehidden
attribute.Here’s a solution from: https://gist.github.com/westonruter/399217
(duplicated here in-case that page disappears)