0

I have the following code to bind some validation logic to be fired when a user updates the value of a textbox. I expect that the //Do some stuff here code will execute when any of the textboxes it is bound to lose focus.

function RegisterHoursValidationHandlers() {
    $('.topic-frame-body input[type=text]').live('change', function () {
        //Do some stuff here
    });
}

This works exactly as I expect in IE, Firefox and Safari. However, the event never fires in Chrome and I have no idea why.

UPDATE: I was able to get the desired effect by changing 'change' to 'blur'. Though this still doesn't explain why it doesn't worh with 'change'.

Hamman359
  • 1,052
  • 2
  • 14
  • 27
  • 1
    If it works when you change it to blur, it is *possible* that you are overwriting the previous event or function. By changing it to blur, whatever is overwriting it no longer will because it's a different event. They are obviously different events. – cgp Jun 15 '11 at 19:01

4 Answers4

2

There's no known quirk about chrome. (the change event is supported across all browsers)

Example with live showing it working against dynamic content.

Test it here:

There is a piece of information or an assumption being made here that makes this unsolvable.

UPDATE: If it works when you change it to blur, it is possible that you are overwriting the previous event or function. By changing it to blur, whatever is overwriting it no longer will because it is a a different event.

This would also explain why you are not seeing any errors. (keep in mind, I believe that jQuery will chain events bound to the same elements, but live() is a bit of a special case - but that fact might point to it being the function, not the event binding)

Tony Stark
  • 8,064
  • 8
  • 44
  • 63
cgp
  • 41,026
  • 12
  • 101
  • 131
0

Try using .delegate() instead http://api.jquery.com/delegate/

I've tried you code in both FF and Chrome - http://jsfiddle.net/B3aRy/ - It worked in both. So maybe its an issue elsewhere in your code?

Alex
  • 8,875
  • 2
  • 27
  • 44
  • I've tried both .delegate() and .change() but neither one gave me a different result. – Hamman359 Jun 15 '11 at 18:12
  • I wouldn't be entirely surprised if it was caused by an issue elsewhere since this is a pretty complicated UI. But if that were the case, I'd expect it not to work in at least one of the other browsers. I'm not getting any errors from the Chrome developer tools and I ran the javascript through JSLint and didn't see anything I didn't expect. – Hamman359 Jun 15 '11 at 18:43
0

What version of Jquery are you using?

I can't see the issue myself, but .live does not support the "change" event until jquery 1.4+

Porco
  • 4,163
  • 3
  • 22
  • 25
0

Try:

function RegisterHoursValidationHandlers() {
    $(".topic-frame-body input[type='text']").live('change', function () {
        //Do some stuff here
    });
}

With the quotes around 'text' as I have it. Worth a shot.

Or try:

$(".topic-frame-body input:text").live();

The point being, I think the problem is in the details of how you're targeting the input field, rather than in the method.

Steph Rose
  • 2,126
  • 3
  • 23
  • 35
  • I tried both of your suggestions, but without luck. Also, thinking that it might be an issue with the field selector, I added a class to the input fields in question and targeted that class directly, ie. $('.foobar'), but that didn't work either. – Hamman359 Jun 15 '11 at 18:34
  • Try this:$(".topic-frame-body > input[type='text']") and the other way too, but using the parent child selector? I really think its in the targeting of the input. – Steph Rose Jun 15 '11 at 18:40
  • Either that, or if you have js code above this function, post that for us too. do you use web developer toolbar in chrome? go to the script tab and see if you are getting any js errors. – Steph Rose Jun 15 '11 at 18:41
  • There is additional js code on the page and I am using the developer tools, but they aren't showing any errors. I also executed the selector `$('.topic-frame-body input[type=text]')` in the console window and it returned a list with all of the elements I expected to see. – Hamman359 Jun 15 '11 at 18:51