Connecting a dojo autocompleteer to a javascript function

advertisements

I am trying to call a java script function when the selected value in a Dojo auto-completer is changed, but I am unable to do so.

Firstly because the standard onchange attribute does not work here, as this is not a standard HTML component.

Secondly I found this documentation ( http://dojotoolkit.org/reference-guide/quickstart/events.html#connecting-to-a-dom-event ) and it is supposed to solve my problem. But somehow I am still not able to connect to a javascript function.

Here is the sample page through which I am trying to test this out.

The JSP:

<s:form id="form">
    <sd:autocompleter id="try" list="sampleList"/>
</s:form>

The JS File:

dojo.connect(dojo.byId("try"),"onchange", tryAlert);

function tryAlert(){
    alert('successful');
}

I don't know what I have interpreted wrong from the documentation.

Please advise.

Thanks!!


Here is what I ended up doing. For those still stuck in a similar situation, this will be helpful.

In the jsp File do this:

<s:form id="form">
    <s:hidden id="chngd"/>
    <sd:autocompleter id="try" list="sampleList" valueNotifyTopics="topic"/>
    ///////////
    //Here you can put more autocompleters if you need them , Like I needed them
    ///////////
</s:form>

In the js file do this:

dojo.event.topic.subscribe("topic", function(){
    dojo.byId('chngd').value='try';// I have set the value of the hidden field to desired value here....
    //whatever more you want to do....
});

//////////
//Here there would be a subscription (similar to above) for each autocompleter you have put in your jsp.
//////////

So what will happen here is that, whenever an autocompleter is changed it will notify or publish a topic for listeners to listen. Now the subscribe function in java script will listen to its respective 'topic' and when that topic is published, the subscribe will execute the javascript function inside it.

This way whenever an autocompleter is changed a respective javascript function is called, thus we have a -- onchange="javascript function" -- kind of effect.

If you still face trouble, ask for help :).