permaculture playing cards*
The moose likes HTML, CSS and JavaScript and the fly likes what does Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "what does "this" refer to" Watch "what does "this" refer to" New topic
Author

what does "this" refer to

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
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

Joined: Jan 10, 2002
Posts: 60766
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
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

Joined: Jan 10, 2002
Posts: 60766
    
  65

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

Joined: Dec 20, 2006
Posts: 89
In your example "this" refers to your input field. "this.value" refers to the value of your input field.


Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does "this" refer to
 
Similar Threads
how to specify composite data in s struts FORM
Readable Text Field
Popup Window from JSP
Ajax foriegn word issue
how to use setTimeout function