Search code examples
javascriptjavajquerystruts2dojo

Detect change of value of struts2 autocompleter tag using javascript or jquery


I am working on Struts 2 application. In my application, I need to use Struts 2 autocompleter tag. For that I have used struts2-dojo-plugin-2.3.1.2.jar jar file. I need to fetch the value from autocompleter once the value changes. I tried using onchange event but it was not working. Here is my code:

<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<script type="text/javascript">
function abc() {
    var a = dojo.widget.byId("country");
    var value1 = a.getSelectedValue();
    document.getElementById("myText").value = value1;
}
</script>
<sx:head />
</head>
<body>
<sx:autocompleter name="country"
id="country" onchange="abc();" list="cricketNations" />
</body>
</html>

How do I achieve this. Help me solve this issue.


Solution

  • The struts2-dojo-plugin is deprecated. You need to use <sj:autocompleter> from .

    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
    <html>
      <head>
        <sj:head jqueryui="true"/>
      </head>
      <body>
       <div id="myText" class="result ui-widget-content ui-corner-all"></div>
    
       <sj:autocompleter name = "country"
                           id = "country" 
               onChangeTopics = "autocompleteChange" 
                         list = "%{cricketNations}" />
       <script>
         $.subscribe('autocompleteChange', function(event, data) {
           var ui = event.originalEvent.ui;
           var message = ui.item.value;
           if (ui.item.key) {
             message = '( '+ ui.item.key +' ) '+message;
           }
           $('#myText').html('&lt;b&gt;'+message+'&lt;/b&gt;');
         });
       </script>
    
      </body>
    </html>