• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what does "this" refer to

 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic