Search code examples
javascriptjakarta-eestruts-1

How do I trigger a struts action from within javascript?


I have searched the web for this but so far got nowhere. I have the following javascript code:

function trigger(){
                var fdate = document.getElementById("d_date").value;
                var m = fdate.substring(fdate.indexOf('/')+1, fdate.lastIndexOf('/'));
                var y = fdate.slice(fdate.lastIndexOf('/')+1);

                form.action="/toEditDate.do?month="+m+"&year="+y+"&navigate";
                form.submit();

            }

an the following jsp element:

<html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
onchange="trigger();"/>

The code is adapted from what I've managed to dig up searching online. Only problem is when I change the text in the textbox nothing happens.


Solution

  • I seems you need to pass in the form instance

    function trigger(form){
                var fdate = document.getElementById("d_date").value;
                var m = fdate.substring(fdate.indexOf('/')+1, fdate.lastIndexOf('/'));
                var y = fdate.slice(fdate.lastIndexOf('/')+1);
    
                form.action="/toEditDate.do?month="+m+"&year="+y+"&navigate";
                form.submit();
    
            }
    

    And the jsp change

    <html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
    styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
    onchange="trigger(document.getElementById('formId'));"/>
    

    or if only one form on the page

    <html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
    styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
    onchange="trigger(document.forms[0]);"/>