Hello, good day, it would help me a lot if you could help me with the following:
'<td idpatente ="'+elem.id_mantencion_tractos_marca+'">'+'<button type="button" name="codigo" id="'+elem.id_mantencion_tractos_marca+'" class="btn btn-outline-primary MostrarFactura" data-bs-toggle="modal" data-bs-target="#MostrarFactura" onclick=Modal('+elem.id_mantencion_tractos_marca+')>VISUALIZAR FACTURA</button> '+'</td>'+
<script>
var aux='';
function Modal(argument) {
aux=argument;
}
console.log(aux);
</script>
<div class="modal fade" id="MostrarFactura" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-fullscreen">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">VISUALIZACION FACTURA MANTENCION</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="text" class="form-control" name="codigo" id="codigo" >
<?php
?>
</div>
</div>
</div>
</div>
I have that button in a table. At the moment of pressing it, it goes to the script of the Moda(argument) function; I need the button id. But I want it to be global. Because I'm going to need it for a consultation. Which is going to be in the modal.
I just think you are underestimating Javascript and that leads you to apply bad practices and could lead you to write wacky code to achieve something simple.
You can replace your
Modal()
in-line function on each button, which is bad practice, with event listening . It would suffice to reach the elements by their type (button
) and by their class (MostrarFactura
) and obtain everything else (in this case the id), by listening to events.Here you have an example, you will see that I have removed this from all the buttons:
onclick=Modal('+elem.id_mantencion_tractos_marca+')
for what has already been said, it is not necessary, and it is a bad practice to do this. You'll see with juste.target.id
you get the id, if you properly implement event listeners.Since using numeric ids is not recommended, if you need numbers, you can use data attributes, or another property of the element (for example the property
value
) to store said data if it is numeric.Here's an example based on data attributes: