Pass button value to modal

Asked

Viewed 658 times

1

I need help with the javascript code, I’m new and I’m not really sure what the syntax is. What I need is to take the value of this button and play on the id_usuario label, as I do?

<button data-target="modal1" class="negar" value="{{item.id}}" class="btn modal-trigger blue"><i class=" large material-icons ">clear</i

    <!-- Modal Structure -->
    <div id="modal1" class="modal">
        <div class="modal-content">
            <h4>Motivo</h4>
            <form method="POST" action="." class="viewform" id="formMotivo">
              <label id=id_usuario> </label>
                <button type="submit" name="salvar" class="btn btn-primary btn-lg">Salvar</button>    
            </form>
        </div>
        <div class="modal-footer"></div>
    </div>
    script>

             $(document).ready(function(){


      **document.getElementById("id_usuario").innerHTML = btn(negar).value;**

             });
  • If it does not depend on user interaction why not put there directly the {{item.id}} ? Or you can take others ids ?

  • It is a for, it has several rows of the table, therefore several ids, when clicking the button of that line it is taking the id of that user

2 answers

0

The @Isac comment is pertinent, because if you don’t need interaction you could just use {{item.id}} directly on label. But I will consider that you just simplified for the question.

I think you could do it like this:

document.getElementById("id_usuario").innerHTML = document.getElementsByClassName("negar")[0].getAttribute("value");

But seeing in your code you using Jquery, I believe it would be better if you standardized the use of it.

Thus remaining:

$("#id_usuario").text($(".negar").attr("value"));

Snippet:

<button data-target="modal1" class="negar" value="{{item.id}}" class="btn modal-trigger blue">
  <i class=" large material-icons ">clear</i>
</button>
<!-- Modal Structure -->
<div id="modal1" class="modal">
  <div class="modal-content">
    <h4>Motivo</h4>
    <form method="POST" action="." class="viewform" id="formMotivo">
      <label id=id_usuario></label>
      <button type="submit" name="salvar" class="btn btn-primary btn-lg">Salvar</button>    
    </form>
  </div>
  <div class="modal-footer"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("#id_usuario").text($(".negar").attr("value"));
  });
</script>

  • Thank you for your contribution. I noticed that nothing is passing to the modal, the value is getting the correct id, but I put some text to see if it enters the input but nothing. I believe it is because I am using the data-target, so it does not need and does not read the code I create below. If I take the data-target I can’t open the modal by onclick

  • @Biancac. I didn’t quite understand your comment. I edited my reply by adding a snippet. It does exactly what you asked, picks up the attribute value and inserts as text from label, without of course taking into account best practices, only second with the code you provided. The modal is obviously not working, would need to add the library that handles it.

0

How you use Jquery, you can use in your button, an attribute of type date-*, turning him into:

<button data-itemid="{{item.id}}" onclick="OpenModalFor(this)"></button>

And recover the value in the function as follows:

function OpenModalFor(clicked_button) {
   var item_id = $(clicked_button).data('itemid');
   $('#id_usuario').text(item_id);
}
  • Hello Bruno, thank you. I would like to ask if you know why this error: Referenceerror: openModal is not defined

  • This error usually occurs when something has not been defined. Make sure you have created the variable/openModal function. Javascript is case-sensitive, so openModal and Openmodal are two different things! And the best way to thank on Stackoverflow is by accepting the answer,!

Browser other questions tagged

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