0

I have the following jQuery code :

$("#btnReset").click(function () {
    ColReorder.fnReset(oTable);
    return false;
});

And the HTML as follows :

<div style="padding: 5px 5px 1px 0px; float: right" id="globalbutton-row" class="ex">
    <button class="button" type="button" id="btnReset"><%=rb.getString("button.reset")%></button>
    <button class="button" type="button" id="btnDeveloperSampleMenu" onclick="javascript:CallDeveloperSampleMenu()"><%=rb.getString("button.devsamplemenu")%></button>
</div>

I want the button 'Reset' to launch the function ColReOrder.fnReset but for some reason this does not work.

5
  • What do you mean by "does not work"? Do you see any errors in your console? Commented Dec 12, 2012 at 18:53
  • Where is your ColReorder.fnReset( oTable ) function? Commented Dec 12, 2012 at 18:53
  • 3
    Did you wrap that in document ready ? Commented Dec 12, 2012 at 18:53
  • Your code seems right. Could there possibly be a problem with the ColReOrder.fnReset function? Commented Dec 12, 2012 at 18:54
  • Always put description about your errors. So other users can identify what problem do you have.. Commented Dec 12, 2012 at 19:11

3 Answers 3

2

You have to be careful to not reference anything in the DOM that isn't loaded yet. If the <button> is not loaded into the DOM before your code is fired, nothing will happen. You can fix this by running your code when everything is loaded:

$(document).ready(function() {
  $("#btnReset").click( function () {
    ColReorder.fnReset( oTable );
    return false;
  } );
});

Also if the <button> is added via AJAX or some other way, you will have the same problem. You can fix this by using the on() method:

$(document).ready(function() {
  $(document).on('click',"#btnReset", function(event) {
    ColReorder.fnReset( oTable );
    return false;
  } );
});
Sign up to request clarification or add additional context in comments.

3 Comments

the live() function has been deprecated in latest jQuery. use .on() instead
That's not how on works. You don't just replace .live() with .on(). They are different. It should be $('#parent').on('click', '#btnReset', function(){, where '#parent' is the closest parent that is always in the DOM (could be document).
My apologies, I thought the selector was simply used to specify which descendants the event was applied to, and was omitted to have it apply to the current object. I was halfway there.
1

try using event delegation. Chances are the element hasn't loaded into the dom when your javascript is getting executed. attaching the click event to the document will make the code work by listening to click and then searching for your selector at the time of the click.

$(document).on('click','#btnReset',function () {
  ColReorder.fnReset( oTable );
  return false;
  } );

Comments

0

You have to execute your code after the document was loaded.

$(function() {
  $("#btnReset").click( function () {
    ColReorder.fnReset( oTable );
    return false;
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.