Marcia Pereira Reis Marcia Pereira Reis - 21 days ago 8
jQuery Question

Pass variable to input text field in a bootstrap modal

Good morning, I need your help. I am using the code below, where I have a table with colors and clicking on the desired color, opens a modal and the input text is filled with the desired color.

$(document).ready(function () {
$("#myModal").on('shown.bs.modal', function (e) {
var color = $(e.relatedTarget).data('number');
$(this).find('#inputColor').val(color);
});
});


It turns out that when using the code above, if the page is loaded for the first time, the value comes null when I click the desired color to go to the modal. On the second try, it works. I've been there for days and I will not solve it.

Answer

I would try to change your code a bit and use:

var color = $(e.relatedTarget).attr('data-number');