tags:

views:

46

answers:

4

How to check my change event fired or not if its fird I need send true if not i need to send false..

$("#selectid").change(function(){
});

I need to check this event is fired or not..?

thanks

+1  A: 
var didF=false;

$("#selectid").change(function(){
   didF=true;
});

...

if (didF) {
   ...
}
mcandre
That won't really work because you're checking the value of `didF` immediately after binding the callback.
Matt Ball
No, ... occurs immediately after binding the callback. if occurs wherever the check needs to be made.
mcandre
+1  A: 

The line of code you included in your question binds a function (a callback or handler) to the change event of the element with ID selectid. Are you trying to determine if your callback fires?

If that's the case, there are a variety of options. If you're using Firebug or another debugger, you can either put a breakpoint somewhere in your callback. You can also use the debugger; keyword to "hardcode" a breakpoint into a specific location, like this:

$("#selectid").change(function(){
    debugger;
});

The most rudimentary way to see when your callback fires is to use an alert in place of the debugger statement:

$("#selectid").change(function(){
    alert("It fired!";
});

I'm not sure what you mean about "sending true or false."

Matt Ball
I need to check in document.ready funtion wheather any changed are made to selectid or not?
The document ready event fires only once per page, and it will generally fire before the user can interact with your page. So, you will really never see a user-induced `change` event fire within a document ready handler. Could you explain in any more detail what you're trying to accomplish?
Matt Ball
+1  A: 

I assume this is related to your other question. I updated my answer in that one since you asked about it there, but I guess I'll post the answer here too.

One way to track which form elements were modified would be through .data().

var array;

$("#fieldset").change(function(event) {
    $(event.target).data('changed',true);
});

$("form").submit(function() {
    array = $(this).find("input, select,textarea").map(function() {
        var $th = $(this);
        if( $th.data('changed') ) return $(this).val();
    }).get();

    alert(array);
    return false;
});​
patrick dw
thanks patrick, the problme is I have submit button in one view and these fielset editable is in other view..
@rockers - When you say "view", do you mean a different page, or just a different part of the current page?
patrick dw
+1  A: 

You can't reliably check if an event has fired from outside the handler.

I assume you're looking for something such as:

var changed = false;

$("#selectid").change(function(){
    changed = true;
});

if (changed) {
    doSomething();
} else {
    doSomethingElse();
}

But, unless the user is insanely fast (and the browser isn't faster still), the if will be evaluated long before the change handler function is actually called. So, doSomethingElse will be called 99.99999999999999% of the time.

However, you can factor that same human input delay into your approach. Rather than post-testing for changes, assume from the start that no changes have occurred. Then, contain your true script in the change handler. But, the key is to handle all dependencies of the change event within the handler.

prepareWithoutChanges();

$("#selectid").change(function(){
    handleChange();
    triggerOtherDependencies();
});

But, I'm afraid, without knowing what you need to accomplish, I can't be anymore specific than this.

Jonathan Lonowski