[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1793
  • Last Modified:

ONLOAD :assign Javascript variable to jsp variable/formfield

ONLOAD :how to assign Javascript variable to jsp variable/formfield ? It gives me  null or not an object error.
0
ram_0218
Asked:
ram_0218
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
rrzCommented:
Please show us your code and explain your problem in more detail.
0
 
ram_0218Author Commented:
<%
      UserInfo userInfo = (UserInfo) request.getSession().getAttribute(VzwConstants.USER_INFO_KEY);
      String s= (String)request.getAttribute("billCycleDate");
            
%>
<html>
<head></head>
<body>
<html:form action="/account/submitForSmsMms.do" method="POST" >
<logic:present name="smsMmsUsageForm">
<bean:define id="smsMmsUsageForm" name="smsMmsUsageForm" type= "com.vzw.edocs.account.action.ViewSmsMmsUsageForm"/>
<bean:define id="stDate" name="smsMmsUsageForm" property="stDate" />
<bean:define id="statementDates" name="smsMmsUsageForm" property="statementDates" type="java.util.ArrayList" />
<bean:define id="billCycleDates" name="smsMmsUsageForm" property="billCycleDates" type="java.util.ArrayList" />


<script type="text/javascript">
            
      array1 = new Array(<%=billCycleDates.size()%>);
            
      function loadCycleDates(){
            
            <%                  
            for (int i=0; i<billCycleDates.size();i++){                        
                        if(billCycleDates.get(i) != null){%>                        
                        array1[<%=i%>] = "<%=billCycleDates.get(i)%>";
             <%}} %>
            var date = <%=s%>;
            alert("hello -  " +date);
            document.smsMmsUsageForm.newcycledate.value=date;
      }      
            
      window.onload = loadCycleDates();

      function showCycleDates(){
            
            document.smsMmsUsageForm.newcycledate.value=array1[document.smsMmsUsageForm.stDate.selectedIndex];
            
      }

</script>


<table>
      <tr><span class="modFormFieldLbl">Statement Date:</span></tr>
      <tr>
            <html:select name="smsMmsUsageForm" property="stDate" onchange="showCycleDates();" style="width: 18%;">
                  <html:options collection="statementDates" property="value" labelProperty="label" />
            </html:select>
            &nbsp;&nbsp;
            <button type="submit" class="gray">DISPLAY</button>
      </tr>
      
      <tr>            
      
            Billing Cycle:                              
                  <input class="hiddenTextBox" name=newcycledate size="26" readonly type=textbox >
            
      </tr>
      
</table>
I am getting error after alert i.e line: document.smsMmsUsageForm.newcycledate.value=date;  b'caz during onload, i guess "newcycledate" is not available.  I need to corresponding value selected in combo box at bottom without submitting i.e at client side itself.
0
 
evnafetsCommented:
You aren't actually running this function with the "onLoad" event.

  window.onload = loadCycleDates();
This line is actually CALLING your loadCycleDates() function while the page is loading.
Suggested solution: remove that line of code.
Change your body tag:

<body onload="loadCycleDates();">

Also by convention, all <script> tags should be included in the head, though they should work whereever you put them.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ram_0218Author Commented:
the onload() script never fired when i placed in  <body onload="loadCycleDates();">
0
 
rrzCommented:
evnafets has made good suggestions. But, I see also that
><input class="hiddenTextBox" name=newcycledate size="26" readonly type=textbox > 
could instead be
<input class="hiddenTextBox" name="newcycledate" size="26" readonly="true" type="text" >
I don't think javascript has  "textbox"         rrz
0
 
actonwangCommented:
ram_0218,

      The  problem is here:

     >> window.onload = loadCycleDates();

      You need to write it as:
 
      window.onload = loadCycleDates;

       
      Try it out and let me know how it goes :)

Acton

0
 
ram_0218Author Commented:
i moved the script block to the end, i.e just before </body></form> closing, instead of beginning since the form field is not loaded by that time.  now i dont see the error.  Appreciate efforts of whoever has responded.
0
 
actonwangCommented:
ram_0218,

    as I said,    you need to write as :
 
      window.onload = loadCycleDates;

   instead of

      window.onload = loadCycleDates(); <-- this is wrong, function will be called possibly before onload. function is not assigned to onload event.

   If you do that, you can move whereever you want.

Acton
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now