Wednesday, September 7, 2011

Bind event on changing selected option using Jquery

Below I am sharing a code , using which we can -

a) add an event
b) remove the event
c) modify the event on changing the option.

First bind the function with change and keyup events (Keyup is required when the user chnges option using his up and down keys)

$("#id").bind('change keyup',function(){
required_func(this);
});


Now, the required function

function required_func(element)
{
if($(element).val() != "")
{
var parent = $(element).parent();

                // action = do something 

// Append.
if ( // no action )
{
// append action
}
// Update.
else
{
$(element).attr();
                        $(element).show();
}
}
// Remove.
else
{
$(element).hide();
}
}


Hope this helps..

The important thing is , it takes into consideration all cases
1) on key up
2) removing action
3)modifying action

No comments:

Post a Comment