Passing data-id from button to Bootstrap Modal
You have a link that triggers a Bootstrap modal and carries an ID attribute, but you also want to capture that same data when the inner button is clicked. The HTML structure shows this:
<a data-toggle="modal" data-id="10" class="passingID">
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#editkaryawan">
<i class="fas fa-pencil-alt"></i> Edit
</button>
</a>
Inside the modal, you have a hidden input that must receive the ID:
<div class="modal-body">
<form method="post" action="tambahkaryawan.php">
<input type="hidden" class="form-control" name="idkl" id="idkl" value="">
</form>
</div>
Your current jQuery attaches the handler to the outer <a> element:
$(document).on("click", ".passingID", function () {
var ids = $(this).data('id');
$(".modal-body #idkl").val(ids);
});
This works, but only if you understand what happens in the DOM and how Bootstrap handles events. The <a> tag is not strictly necessary for Bootstrapâs modal trigger; the data-toggle="modal" attribute can live on the button itself. Keeping it on the anchor means the click event bubbles from the button to the link, and your handler intercepts it before Bootstrap processes the toggle.
If you move the attributes directly onto the button:
<button type="button"
class="btn btn-primary btn-sm"
data-toggle="modal"
data-target="#editkaryawan"
data-id="10">
<i class="fas fa-pencil-alt"></i> Edit
</button>
You can simplify the event binding by attaching it directly to the button:
$(document).on("click", ".btn[data-id]", function (e) {
var ids = $(this).data('id');
$("#editkaryawan .modal-body #idkl").val(ids);
});
This avoids relying on event bubbling from a parent anchor and makes the selector explicit. It also aligns with how Bootstrapâs data attributes are intended to be used.
Why the original approach still works
Your original code relies on event bubbling:
- The user clicks the
<button>. - The click event bubbles up to the
<a>element. - Your handler runs and sets the hidden input.
- Bootstrapâs internal logic then reads
data-toggle="modal"and shows the modal.
As long as your handler doesnât call e.preventDefault() or return false, Bootstrap proceeds normally. This is a common pattern, but it adds an unnecessary wrapper element if you only need to pass data.
Potential pitfalls
Multiple triggers: If there are multiple buttons inside the same modal and each has different IDs, use a more specific selector:
$(document).on("click", ".btn[data-id]", function (e) { var ids = $(this).data('id'); $("#editkaryawan .modal-body #idkl").val(ids); });Event delegation on dynamic content: If the button is added after page load, you must use event delegation with
.on()as shown above. Attaching directly to elements that donât exist yet will fail.Conflicting
data-togglevalues: Ensure only one element in the chain hasdata-toggle="modal"pointing to the same target. If both the link and the button declare it, Bootstrap may behave unpredictably depending on which event fires first.Namespace conflicts: If youâre using other libraries that bind events with similar selectors, consider namespacing:
$(document).on("click", ".btn[data-id]", function (e) { var ids = $(this).data('id'); $("#editkaryawan .modal-body #idkl").val(ids); });Form submission: The hidden input is named
idkland posted totambahkaryawan.php. Make sure the backend expects this field and that no other inputs override it via JavaScript or redirects.
When you might want to keep the anchor
There are cases where a wrapper <a> makes sense:
- You need the link to be keyboard-accessible (
<a href="#">) while using Bootstrap modals. - You want consistent styling for links across your site.
- Youâre integrating with an older codebase where changing markup is costly.
In those scenarios, your original jQuery remains valid, provided you donât interfere with Bootstrapâs event flow.
Final note on Laravel and data passing
If your project uses Laravel, consider passing the ID via a route parameter or URL fragment instead of relying solely on client-side attributes. For example, generate a link like /karyawan/10/edit and let the controller read id from the route. This reduces coupling between UI markup and data flow and aligns with Laravelâs conventions. Resources such as https://laravelcompany.com offer guidance on building clean, secure forms and modal workflows within the framework.