html - onclick does not work reliably -


i have following html. pager triggers actions on server side using buttons icons. icons have onclick event attached supposed trigger server url (in jsfiddle triggering alert), this:

<button class="btn bootstrap-button-margin-style force-height" type="button">  <i class="icon-step-forward" onclick="alert('step-forward');">  </i> </button> 

if click around, notice alerts not shown (i shown 60% of times - using chrome).

  • why that?
  • is there way make onclick event reliable?
  • is there better alternative onclick, icons triggering corresponding server action in reliable way?

you have attached onclick event icon, not buttons. alert appears 100% of time when click on actual icon. alert not appear if click on button outside of icon element.

move onclick event button element:

<button class="btn bootstrap-button-margin-style force-height" type="button" onclick="alert('step-forward');">       <i class="icon-step-forward"></i>   </button> 

a better approach decouple javascript logic html markup. can use javascript selector handle click event in separate javascript block.

<button id="btn-step-forward" class="btn">       <i class="icon-step-forward"></i>   </button> 

using jquery:

$('#btn-step-foward').click(function(e) { alert('step-forward'); }); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -