richiej Posted September 14, 2010 Share Posted September 14, 2010 Hi,I've looked all over the web for an explanation on how to use the "this" operator. It's almost impossible because the word "this" is so common. The examples I found are mixed up in the authors coding and are not at all clear. Thanks,Rich J Link to comment Share on other sites More sharing options...
ShadowMage Posted September 14, 2010 Share Posted September 14, 2010 "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 More sharing options...
aspnetguy Posted September 14, 2010 Share Posted September 14, 2010 Quirksmode covers this well. In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of.check out the whole tutorial http://www.quirksmode.org/js/this.html Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.