Calling JavaScript functions from form controls

Products:   
Areas: ASP, ASP.NET(C#), ColdFusion, Java Servlets, JSP, Perl, Perl 

JavaScript code is often used to provide interactivity by responding to client side events which cannot otherwise be processed by server side code. For instance, JavaScript code can be used to perform some action when a control gains focus or when the value of a control is changed. While is possible to write compact JavaScript code that is included within the HTML tag of a control e.g.

<input name="test" value="test control" OnChange="JavaScript:alert('You have changed the field value.');">

sometimes is necessary to write functions which execute a series of steps. When in HTML mode, you can add JavaScript functions to the page code by using the <Script> tag e.g.

<script language="JavaScript">

function deleteConfirm() {
if (document.formname.value != 3)
{
alert("You are not allowed to perform the delete operation");
return false;
}
else
return true;
}

</script>

The above code snippet declares a JavaScript function called deleteConfirm() that can be called from one of the events of a control. To illustrate this, the delete button below invokes the deleteConfirm() function when it is clicked:

<input type="submit" value="Delete" name="Delete" onclick="JavaScript:return deleteConfirm();">

As such, depending on the evaluation performed in the function, the click event of the delete button will either be successful or not.



Viewed 64589 times.   Last updated: 11/19/2002 1:41:20 AM