1
I have the following javascript code in my project that picks up attribute values when the user clicks the class button rename
and calls a modal:
<script type="text/javascript">
$('.rename').on('click', function () {
var $this = $(this);
var nome = $this.attr('data-name');
var id = $this.attr('data-id');
document.getElementById("nome_marca").innerHTML = nome";
$('#id_marca').attr("value", id);
$('#myModal').modal('show');
});</script>
In the modal, briefly, I have the following structure
Link the user will click on the table to call the modal:
<a class="rename" data-toggle="modal" href="#myModal" data-name="@Html.DisplayFor(model => item.nomeMarca)"
data-id="@Html.DisplayFor(model => item.id)"><span class="glyphicon glyphicon-edit">
</span>Renomear</a>
Modal Header:
<h4 class="modal-title">Digite o novo nome para a Marca <span id="nome_marca"></span></h4>
Rename form:
<form method="post" id="form_rename" action="/Marca/RenomearMarca">
<div class="modal-body">
<div class="form-group">
<input type="hidden" name="idMarca" id="id_marca" />
<input type="text" name="nomeMarca" required class="form-control" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<input type="submit" class="btn btn-primary" value="Renomear"/>
</div></form>
I want when the user clicks on the class button rename
it takes the values of the attributes data-name
and data-id
and send them to the modal. Making the id span nome_marca
be shown, for example, "Enter the new name for the [Asus] tag" and the input hidden
receive the value of the attribute data-id
.
I have tried every way but the values are not going through. What is missing in the code to work?
Hello Aline. I put in the code where the id and name are coming from. I put the same way you gave me but you are not setting the values :S
– WitnessTruth
Show us the element code with the class: "Rename", please, @Renankaiclopes
– Aline
I already put in the post. Check if updated there for you
– WitnessTruth
Swap this line: $("#form_rename #name_tag"). text(name); for $("#form_rename #name_tag"). text("test"); and tell me what happened.
– Aline
I made a change to the post. Your H4 is out of the form, right? So just remove the id from the form when you pass the text value.
– Aline
Exactly Aline. I made the changes as you passed, but it does not work :( is not passing to the modal. The strange thing is that by Fiddle the same code WORKS.
– WitnessTruth
And are you entering the click event? If you put a Debugger there? or a log console, print?
– Aline
Let’s go continue this discussion in chat.
– Aline