Search code examples
javascripthtmljspjsp-tags

How to pass parameters to javascript function that are defined outside the form tag?


i am trying to pass a parameter to a javascript function thats been defined outside the tag.but when i try to use it in the javascript function it shows undefined.i am using alert to print the value both in the jsp page and in javascipt function...please help

<html>
<script type="text/javascript">
js_valueDate = '<%=valueDate%>'; 
alert(js_valueDate)     **//displays correct value here**

 </script>
   <body>
   <form>
 ....some html...
     <td width=27%><input type=text name="ValDate" 
onchange = "javascript:validateDate(document.f1.ValDate,js_valueDate);"></td>   
......some html....                                                                                               
  </form>
  </body>
  </html>

and this is my javascript function:

function validateDate(ValDate,origValDate) {
  var valueDate=ValDate.value;
  var OrigvalueDate=origValDate.value;
  confirm(valueDate);
  confirm(OrigvalueDate);  **//displays undefined here**
  var hh=replaceAll(valueDate,'-','');
  confirm(hh);

  if (replaceAll(valueDate,"-","")<=valueDate<=replaceAll(OrigvalueDate,"-","")) {
    return true;
  } else {
   alertPopup("Please enter a valid value date");
   document.f1.ValDate.focus();
   return false;
  }

}

Solution

  • Since you are passing the value itself there is no need of the statement
    var OrigvalueDate=origValDate.value;
    Here is a small example which i have written which explains both the situation

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <title>Check </title>
      <script>
      function display(v)
      {
      var d=v.value;
        alert(v);
        alert(d);
      }
       jval="qwerty";
      </script>
      </head>
      <body>
        <input type="button" value="check" onclick="javascript:display(jval)"/>
      </body>
    </html>