Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

JavaScript: this Operator

Description

The this operator is used to refer the current object. In general, this refers to the calling object in a method.

Syntax

this.propertyName

Parameter

propertyName: The name of the property.

Example:

In the following web document, this operator is used as internal reference property of the form instances.

HTML Code

<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript this operator example with DOM
</title>
<meta name="description" content="This document contains an
example of JavaScript this operator"/>
</head>
<body>
<form name="myform" action="#">
<input type="text" value="Text Here" name="text1" />
<input type= "submit" value="Submit" name="mysubmit" onclick=
"formdetails(this.form)" />
</form>
<script src="javascript-this-operator-example1.js">
</script>
</body>
</html>

JS Code

function formdetails(form)
{
var newParagraph = document.createElement("p"); //creates a new
paragraph element var newText = document.createTextNode("The name
of the form is: "+form.name); //creates text along with ouput to be
displayed 
newParagraph.appendChild(newText); //created text is appended to the
paragraph element created document.body.appendChild(newParagraph);
// created paragraph and text along with output is appended to the
document body


var newParagraph1 = document.createElement("p"); //creates a new
paragraph element var newText1 = document.createTextNode("The
deault value of text box is: "+form.text1.value); //creates text
along with ouput to be displayed newParagraph1.appendChild
(newText1); //created text is appended to the paragraph element
created document.body.appendChild(newParagraph1);
// created paragraph and text along with output is appended to the
document body

var newParagraph2 = document.createElement("p"); //creates a new
paragraph element var newText2 = document.createTextNode("The name
of the submit button box is: "+form.mysubmit.name); //creates text
along with ouput to be displayed newParagraph2.appendChild(newText2);
//created text is appended to the paragraph element created
document.body.appendChild(newParagraph2);
// created paragraph and text along with output is appended to the
document body

var newParagraph3 = document.createElement("p"); //creates a new
paragraph element var newText3 = document.createTextNode("The deault
value of submit button is: "+form.mysubmit.value); //creates text
along with ouput to be displayed newParagraph3.appendChild(newText3);
//created text is appended to the paragraph element created
document.body.appendChild(newParagraph3);
// created paragraph and text along with output is appended to the
document body
}

View the example in the browser

See also

Conditional Operator
comma
delete
function
in
instanceof
new
typeof
void

Previous: JavaScript: new Operator
Next: JavaScript: typeof operator

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

How to insert an item into an array at a specific index (JavaScript)?

What you want is the splice function on the native array object.

arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:

var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());

Ref: https://bit.ly/2BXbp04