Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Using the Change feature in jQuery with Chosen

    All,
    I'm using the Chosen plugin (http://harvesthq.github.com/chosen/). It says that I can use the following for any change events:

    Form Field Change
    When working with form fields, you often want to perform some behavior after a value has been selected or deselected. Whenever a user selects a field in Chosen, it triggers a "change" event* on the original form field. That let's you do something like this:
    Code:
    $("#form_field").chosen().change(  );
    Note: Prototype doesn't offer support for triggering standard browser events. Event.simulate is required to trigger the change event when using the Prototype version.

    However, when I do this in my code and try and do something like this:
    Code:
    $("#form_field").chosen().change( alert("this is an alert"); );
    Or Something like this:
    Code:
    $("#form_field").chosen().change({ alert("this is an alert"); });
    It never gives me an alert and actually makes the rest of my jQuery not work. Can anyone figure out how to use this change feature? Any advice is appreciated!

    Thanks!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    The first parameter of the .change() method has to be a function reference rather than a call to a function.
    Code:
    $("#form_field").chosen().change( function() {alert("this is an alert"); });

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Thanks, that worked great. However I do have another question. Does Chosen pass an action with the change? For example, it calls the change event but I only want something to happen if I clicked the remove button. Is there any way to determine that action in this function?

    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •