Jump to content

Javascript "this" statement


richiej

Recommended Posts

"this" is refers to (I believe) the parent of the calling function. So as a simple example, consider this code:

<script type='text/javascript'>function showValue(elem) {   alert(elem.value);}</script>.....<input type='text' name='test' value='This is a test' onchange='showValue(this);' />

When the value of the input is changed, the function showValue will run with this as the parameter. In this case the calling function is the onchange function which in turn runs the showValue function. The parent of the onchange function is the input element, so the element is passed as a parameter to the showValue function, which will then alert the value of the input.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...