JAVA SCRIPT FUNCTIONS

 

 

GET ELEMENT BY ID:

One of many JavaScript HTML methods is getElementById().

The example below "finds" an HTML element (with id="demo"), and changes the element content (innerHTML) to "Hello JavaScript":

document.getElementById("demo").innerHTML = "Hello JavaScript";  

 JavaScript accepts both double and single quotes.

 

JavaScript Can Change HTML Attribute Values

In this example JavaScript changes the value of the src (source) attribute of an <img> tag:

 <button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>

<img id="myImage" src="pic_bulboff.gif" style="width:100px">

<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>


JavaScript Can Change HTML Styles (CSS)

Changing the style of an HTML element, is a variant of changing an HTML attribute:

document.getElementById("demo").style.fontSize = "35px";  

 

JavaScript Can Hide HTML Elements

Hiding HTML elements can be done by changing the display style:

document.getElementById("demo").style.display = "none";  

 

JavaScript Can Show HTML Elements

Showing hidden HTML elements can also be done by changing the display style

document.getElementById("demo").style.display = "block";  

 

 

The <script> Tag

In HTML, JavaScript code is inserted between <script> and </script> tags.

<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>

You can place any number of scripts in an HTML document.

Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.

 
 
 

<!DOCTYPE html>

<html>
<body>

<h1>My First Web Page</h1>
<p>My First Paragraph</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html>
 

JavaScript Functions and Events

A JavaScript function is a block of JavaScript code, that can be executed when "called" for.

For example, a function can be called when an event occurs, like when the user clicks a button.

 

 <html>

<head>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>

<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>

</body>
</html>

OR

<!DOCTYPE html>
<html>
<body>

<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>

</body>
</html>
 

External JavaScript

function myFunction() {
  document.getElementById("demo").innerHTML = "Paragraph changed.";
}

 

External scripts are practical when the same code is used in many different web pages.

JavaScript files have the file extension .js.

To use an external script, put the name of the script file in the src (source) attribute of a <script> tag: 

<script src="myScript.js"></script> 
 
External scripts cannot contain <script> tags.
 

External References

External scripts can be referenced with a full URL or with a path relative to the current web page.

This example uses a full URL to link to a script: 

<script src="https://www.w3schools.com/js/myScript1.js"></script>

This example uses a script located in a specified folder on the current web site:

  <script src="/js/myScript1.js"></script>

 

JavaScript Display Possibilities

JavaScript can "display" data in different ways:

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().
  • Writing into an alert box, using window.alert().
  • Writing into the browser console, using console.log().

 

 

Using document.write()

<script>
document.write(55 + 6);
</script> 


Using document.write() after an HTML document is loaded, will delete all existing HTML:

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My first paragraph.</p>

<button type="button" onclick="document.write(5 + 6)">Try it</button>

</body>
</html> 

The document.write() method should only be used for testing.

 

Using window.alert()

You can use an alert box to display data:

<!DOCTYPE html>

<html>

<body>
<h2>My First Web Page</h2>

<p>My first paragraph.</p>
<script>

window.alert(5 + 6);

</script>
</body>

</html>

You can skip the window keyword.

In JavaScript, the window object is the global scope object, that means that variables, properties, and methods by default belong to the window object. This also means that specifying the window keyword is optional:

 <!DOCTYPE html>

<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
alert(5 + 6);
</script>
</body>
</html>

 

Using console.log()

For debugging purposes, you can call the console.log() method in the browser to display data.

<!DOCTYPE html>
<html>
<body>

<script>
console.log(5 + 6);
</script>

</body>
</html> 

JavaScript does not have any print object or print methods.

You cannot access output devices from JavaScript.

The only exception is that you can call the window.print() method in the browser to print(like xerox) the content of the current window.
 
<!DOCTYPE html>
<html>
<body>
<h2>The window.print() Method</h2>
<p>Click the button to print the current page.</p>
<button onclick="window.print()">Print this page</button>
</body>
</html>
 

https://www.w3schools.com/js/js_statements.asp
https://www.w3schools.com/js/js_statements.asp

 

 

 

 

 
 

 

Comments

Popular posts from this blog

IMPORTANT INTERVIEW RELATED QUESTION

Survey Paper

C++ Interview Questions

Git Cheat Sheet: A Beginner to Intermediate Guide

Java Interview Questions

INTERVIEW QUS ON AUTOMATA

OPERATION RESEARCH SYLLABUS

CPU CACHE MEMORY

MAKING POWERPOINT SLIDES