0

I have the following HTML code (I'm using Bootstrap Twitter Modal):

 <a href='#' data-toggle='modal' data-target='#myModal' id='g' data-id=1>Launch modal</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

And the following JQuery code:

   <script>
              $('#g').click(function () {
                  alert($(this).data("id"));
              });
   </script> 

I want that onclick the link, after the bootstrap modal will open, there will be alrt with the 'data-modal' value.

I tried the code above and it didn't work.

My question is how can I fix it? (I'm pretty new with JQuery...)

kjhughes
  • 89,675
  • 16
  • 141
  • 199
Nave Tseva
  • 808
  • 7
  • 22
  • 46

1 Answers1

0

You should place it inside $(document).ready(function (){});

See here for details.

Matthew Daly
  • 8,369
  • 2
  • 36
  • 74