0

I do refers to this fiddle and this post but this doesn't solve my problems. I'm well informed since I used CSP, onclick attribute is not working, thus I use jQuery .on('click') to get this done but still no satisfied results. Here is the structure of my code(will include only codes related to this issues):-

html

<td><input value="Resend" class="btn btn-primary btn-xs" id="resend_sms" name="resend_sms" type="button"></td>

script

$(function(){
            $('#resend_sms').on('click', function(e){
                console.log('clicked');
            });
        });

Above is the codes related. For your notice, this part of html is generated by server-side script. Again, I re-test the this guy but still no results upon clicked.

Community
  • 1
  • 1
Wafie Ali
  • 228
  • 9
  • 24

2 Answers2

1

Its working fine. I just added the jquery file to your code

$(function() {
  $('#resend_sms').on('click', function(e) {
    console.log('clicked');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><input value="Resend" class="btn btn-primary btn-xs" id="resend_sms" name="resend_sms" type="button"></td>
vijay
  • 926
  • 7
  • 15
  • I am using bootstrap template & script calling as above already called in header file. anyway, kindly appreciated your helps. – Wafie Ali Mar 30 '17 at 05:20
1

If your part of html is dynamic generated or it is generating from server side script:

Then use it like:

       $(function(){
            $(document).on('click','#resend_sms', function(e){
                console.log('clicked');
            });
        });

This will work.

Sagar Arora
  • 1,743
  • 1
  • 10
  • 19
  • Great! I used this answer & it's working. Do you mind if I would like to know the reason behind this? Just to improve my logical thinking on this kind of stuff. Thanks. =D – Wafie Ali Mar 30 '17 at 05:19
  • 1
    @WafieAli as all the jquery event are associated with the document so we have to use document method for dynamic added html. for more details you can visit http://stackoverflow.com/questions/14879168/document-onclick-id-function-vs-id-onclick-function – Sagar Arora Mar 30 '17 at 05:45
  • Thanks buddy for this sharing! I will keep this for future references. – Wafie Ali Mar 30 '17 at 06:00