Home
 

‘isNaN()’ Example

The isNaN() function determines whether a value is an illegal number (Not-a-Number). This function returns true if the value is NaN, and false if not.

1
2
3
4
5
6
7
8
9
10
11
<html>
<body>
   <button onclick="alert(isNaN('12.34'));">isNaN False</button>
   <button onclick="alert(isNaN('Hello'));">isNaN True</button>
</body>
</html>

‘innerText’ Example

Get text-only content from a div element using .innerHTML and .innerText.

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
   <body>
      <div id="myP">Sample Text inside a <b>p</b> element</div>
      <button onclick="alert(myP.innerHTML);">InnerHTML</button>
      <button onclick="alert(myP.innerText);">InnerText</button>
   </body>
</html>

Output for 1st one:
Sample Text inside a <b>p</b> element

Output for 2nd one:
Sample Text inside a p element

‘fromElement’ Example

Get text-only content from a function call using .innerText.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>
<head>
<script>
    function function1() {
        alert(event.fromElement.innerText);
    }
</script>
</head>
<body><table border="1">
<table>
    <tr><td onmouseover="function1();">CELL 1</td></tr>
    <tr><td onmouseover="function1();">CELL 2</td></tr>
</table>
</body>
</html>

Output on alert box:
CELL 1
CELL 2

‘String’ Example

The String object is used to manipulate a stored piece of text. String objects are created with new String().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<body>
<div id="myDiv" style="width:200; height:100;">This is some sample text</div>
<script language="javascript">
function function1(){
    var temp = new String();
	temp = myDiv.innerText;
	alert(temp);
}
</script>
<button onclick="function1();">String function</button>
</body>
</html>

Output on alert box:
This is some sample text

‘style’ Example

Changing the style of a div element to be highlighted with a background color.

1
2
3
4
5
6
7
8
9
10
11
<html>
<body>
<div id="myDiv" style="width:100; height:100;"></div>
<button onclick="myDiv.style.backgroundColor = 'blue';">Change color</button>
</body>
</html>

‘Object’ Example

Objects work so well because they act just like real life objects- objects have properties and methods. Now I will give you an example showing you how to create objects, Constructor and methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<html>
<body>
<script language="javascript">
function Object(firstName, lastName){
    this.firstName = firstName;
    this.lastName = lastName;
}
function function1(){
    myObject.age = function3;
    alert(myObject.age());
}
function function2(){
    myObject.favoriteColor = 'Blue';
    alert(myObject.favoriteColor);
}
function function3(){
    return 34;
}
var myObject = new Object('Joe', 'Smith');
</script>
<button onclick="function2();">Add Property</button>
<button onclick="function1();">Add Method</button>
<button onclick="alert(myObject.constructor);">Constructor</button>
<button onclick="alert(myObject.isPrototypeOf(myObject));">PrototypeOf</button>
<button onclick="alert(myObject.hasOwnProperty('lastName'));">HasProperty</button>
</body>
</html>

 
 
 

Categories

Tag