0
My dears,
I have a table of users, and for each user I have a button that opens a modal:
{% for item in lista_usuarios %}
<tr>
<td>{{ item.first_name }}</td>
<button data-target="modal1" class = "oi" type = "button" value="{{item.id}}" class = "btn waves-effect waves-light blue" data-index="{{item.id}}" onclick="$('#modal1').modal('open');" >
<i class="large material-icons">clear</i>
</button>
</td>
</tr>
As you can see, the value of this button is already getting the id of each user, this is correct.
Inside the modal, I have a label and I need the value of this id to be passed to her.
<input type="text" class="usuario">
<script>
$(document).ready(function(){
$(".oi").each(function() {
$(this).click(function() {
alert("teste");
$('.modal').modal();
var id = $(".oi").val();
alert(id);
$(".usuario").val(id);
});
});
});
</script>
The modal is working and the 2 Alerts are working. But is passing the same user id always, is passing only the id of the first user to the label inside the modal.
Instead of doing
var id = $(".oi").val()
, try to dovar id = $(this).val()
– Woss
Thank you Anderson, that’s all! But you know tell me because the way I did wasn’t working out?
– Bianca C.
Bianca, yes, because doing
$(".oi")
you will find all elements of the page that have the classoi
and making.val()
the value of the first item in the list will always be returned. With$(this)
you select only the element you want and thus,.val()
returns the desired value.– Woss