This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does "this" refer to

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose in a html text filed I want to add a javascript

<input type=text name=... value=.. onxxxx=javascript:someMethod(this)/>

what I want to do is to pass what user enters in this text field to a javascript mathod called "someMethod" for checking. I simply use "this" here. Does javascript know I am passing the value of this text field ? Is this a valid way to specify ? Could it interpret "this" as a broader scope, like "document", etc ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben oliver:
<input type=text name=... value=.. onxxxx=javascript:someMethod(this)/>

Firstly, that's not valid syntax. You do not use the javascript: pseudo-protocol prefix when specifying code for event handlers.

In any function, this is a reference to the function's function context. In the case of an event handler, it's the DOM element that triggered the event.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

Firstly, that's not valid syntax. You do not use the javascript: pseudo-protocol prefix when specifying code for event handlers.

In any function, this is a reference to the function's function context. In the case of an event handler, it's the DOM element that triggered the event.


Bear, can you make it straight ? in my example, does "this" refer to this text input field value ? I don't know what DOM element is, please use idiot language when you deal with idiots.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben oliver:
Bear, can you make it straight ?
Omit the "javascript:" prefix. That pseudo-protocol is only necessary when substituting script for a URL (something I think is sloppy and imprecise and never do).

in my example, does "this" refer to this text input field value ?
In that example, yes.

I don't know what DOM element is, please use idiot language when you deal with idiots.
The DOM is the Document Object Model built by the elements in your HTML. In your example, the input markup results in an input DOM element.
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example "this" refers to your input field. "this.value" refers to the value of your input field.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic