views:

387

answers:

2

Our design agency has supplied HTML that uses NiceForms. The problem I am having is that this mucks up jQuery event binding.

I have the following code:

keys = $("#key input");
$(keys).each(function(){
  $(this).change(function() {
    console.log("hi");
  });
});

If I disable NiceForms this code works but with Niceforms enabled it doesn't. How do I get around this problem?

+2  A: 

Fix the typo in your code, see if it works then:

keys = $("#key input");
$(keys).each(function(){
  $(this).change(function() {
    console.log("hi");
  });   // <-- oops
});     // <-- oops

I tested with the NiceForms demo using:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;

And the selector keys = $(".niceform input"); and after fixing the typos it works as expected. You are enclosing the code in a $(document).ready(function () {} handler, right? #key is a proper selector for an element enclosing the form input elements, right?

ghoppe
Sorry the typos was only in the code I had written here (I didn't copy and paste it.) Anyway I am still experiencing the same problem
Michael Edwards
As I mentioned, I tested your code with the NiceForms demo download and it worked as expected, so we're missing some info. I recommend you post your html or a link to a live demo showing the problem.
ghoppe
A: 

Ok it turned out that the problem was that the Niceforms image when clicked does not raise the onchange event of the underlying checkbox.

To raise the underlying event find the inputCheck function in NiceForms.js and alter the following (this assumes you are using jQuery):

  el.dummy.onclick = function() {
    if(!this.ref.checked) {

        this.ref.checked = true;
        $(this.ref).change(); //added
        this.className = "NFCheck NFh";
    }
    else {

        this.ref.checked = false;
        $(this.ref).change(); //added
        this.className = "NFCheck";
    }
}
Michael Edwards