0

I want to get the parameter when I click a button on modal.

<div class="modal hide fade" id="resetPasswdModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel1">reset</h3>
  </div>
  <div class="modal-body">
    <input id="Password1" type="password" />
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">cancel</button>
    <button class="btn btn-danger" data-dismiss="modal" onclick="resetPasswd()">confirm</button>
  </div>
</div>

The modal is triggered by:

<a href="#resetPasswdModal" data-toggle="modal" data-id="111" class="btn">reset</a>

When I click confirm on modal, I want to get the value of data-id. This is how I did:

<script type="text/javascript">
  function resetPasswd() {
    var UserID = $('#resetPasswdModal').data('id');
  }
</script>

Then UserID is set to undefined.

How could I get data-* in the javascript

Rajesh
  • 24,354
  • 5
  • 48
  • 79
oleotiger
  • 105
  • 2
  • 11
  • Because `resetPasswdModal` does not have any attribute `data-id`. Also you can refer following post for [data-*](http://stackoverflow.com/questions/4187032/get-list-of-data-attributes-using-javascript-jquery) – Rajesh Apr 23 '16 at 09:37
  • You can use a hidden field with name id in model form and get value from this. – Ranjeet Singh Apr 23 '16 at 09:40
  • I think you have to toggle the modal "manually" take a look at this post http://stackoverflow.com/questions/10379624/how-to-pass-values-arguments-to-modal-show-function-in-bootstrap – rick Apr 23 '16 at 09:42
  • Even better http://stackoverflow.com/questions/10626885/passing-data-to-a-bootstrap-modal – rick Apr 23 '16 at 09:46

3 Answers3

0

You need to add the data-id attribute to your div, not your link:

<div class="modal hide fade" id="resetPasswdModal" data-id="111" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

I also recommend you add a class to your confirm button rather than use onclick:

<button class="btn btn-danger" data-dismiss="modal" id="confirm">confirm</button>

Then you can access the value like so:

$(document).ready(function() {
    $( '#confirm' ).click(function() {
       var UserID = $('#resetPasswdModal').attr('data-id');
    });
});
0

You are checking wrong element, hence you are not getting UserID. Following element(resetPasswdModal) does not have any attribute data-id.

<div class="modal hide fade" 
     id="resetPasswdModal" 
     tabindex="-1" 
     role="dialog" 
     aria-labelledby="myModalLabel" 
     aria-hidden="true">

Your data-id attribute is associated to anchor tag that initiates modal.

<a href="#resetPasswdModal" 
   data-toggle="modal" 
   data-id="111" 
   class="btn">reset</a>

In this situation, you can set UserID on click of a tag and then reset on cancel click. Same is depicted in following fiddle.

JSFiddle

Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

use below code and u will get the data-id value inside the model same technique you can use in many forms

  function ShowModal()
        {


            $("#resetPasswdModal").modal("show")
            $("#resetPasswdModal").on('shown.bs.modal', function (e)
            {
                var mypostid = $("#myid").data('id');
                $(".modal-dialog #idtest").val(mypostid);
            });


        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>


<a href="#" data-toggle="modal" id="myid" data-id="111" class="btn"  onclick="ShowModal();">reset</a>

<div id="resetPasswdModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
          <input id="Password1" type="password" />
    <input type="text" id="idtest"/>
      </div>
      <div class="modal-footer">
      <button class="btn" data-dismiss="modal" aria-hidden="true">cancel</button>
    <button class="btn btn-danger" data-dismiss="modal" onclick="resetPasswd()">confirm</button>
      </div>
    </div>

  </div>
</div>