Changing values in Modal - C# MVC Javascript

Asked

Viewed 637 times

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?

1 answer

5


<script type="text/javascript">
$(document).ready(function(){
    $(document).on("click", ".rename", function() {
        var $this = $(this);
        var nome = $this.data('name');
        var id = $this.data('id');
        $("#nome_marca").text(nome);  
        $('#id_marca').val(id); 
        $('#myModal').modal(); 
    });
});
</script>

Taking into account that the clicked element has date: name and id attributes.

I suggest you publish the part of the code where you get those values too, then it’s easier. (=

  • 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

  • Show us the element code with the class: "Rename", please, @Renankaiclopes

  • I already put in the post. Check if updated there for you

  • Swap this line: $("#form_rename #name_tag"). text(name); for $("#form_rename #name_tag"). text("test"); and tell me what happened.

  • 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.

  • 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.

  • And are you entering the click event? If you put a Debugger there? or a log console, print?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.