views:

107

answers:

3

I have a $.change() event, but when a submit button is pressed, the change event fires again. It is only supposed to fire once when a text is inputted into a input text box.

$('input:submit', top.document).bind('click', function (e) {
    alert("submitting")
});

$('input').change(function (e) {
    alert("fire");
});
+4  A: 

Edit: For whatever reason, a change event is invoked when the input button is clicked. (thanks Anthony)

The way to fix this is simply don't select the submit button.

You can try

$('input:text')

To select only text fields.

Or you can do

$('input:not(:submit)')

To select all input elements except the submit button(s).

Read about selectors here

Edit: Using <button> instead won't work. It still counts as an input field, but it's value is separate from the text displayed on the button.

Mark
yeah it doesn't seem to work. i have edited the code up there to give more info.
ajowi
worked. some typo error on my part.
ajowi
A: 

$('input').change(function(e){ alert("fire") }); applies that event to ALL input elements, including <input type="submit".../>. If you really want EVERY SINGLE text input element to have a change event, you want `$('input[type=text]').change(function(e){ alert("fire") }); Otherwise, it might be best to use an id or class.

notJim
A: 

@Mark,

You are spot on and I'd edit you if I could to help out. Maybe someday soon...

@ajowi,

Submit buttons are inputs. At least most of them are:

  <input type="submit" />

So Mark,it's not that they are doing anything with the button text, it's that the input, which is a button, is being changed by act of clicking on it.

So his solutions were great. Go with

  $("input:text").change
Anthony
That won't work actually, the space means "descendant". Therefore your selector is looking for text inputs that are descendant of another input, which isn't likey to work. Remove the space and it's all good.
TM
TM was right. this code did not work.
ajowi
I balked. I think I've had issues with that before, so I got nervous thinking the space would cover it both ways. Thanks for the heads up.
Anthony
yeah but even with this...i still dont understand why its selecting ALL inputs. even though i clearly specified not to select :submit and only :text
h34y
@ajowi, try it now.
Anthony
You don't specify only text, in the code you provide. You specify select somewhere else, but that's not the same. Not only does that not eliminate the submit from a plain input selector, you also have to remember there are checkboxes, resets, and radio inputs as well. So you have to be specific every time.
Anthony