Difference between revisions of "JavaScript"

From Sinfronteras
Jump to: navigation, search
(Methods)
(Some of the most important methods)
Line 64: Line 64:
 
<br />
 
<br />
 
====Change HTML Attribute Values====
 
====Change HTML Attribute Values====
This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":
+
In this example JavaScript changes the value of the src (source) attribute of an <img> tag:
  
 +
<syntaxhighlight lang="javascript">
 
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>
 
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>
  
Line 71: Line 72:
  
 
<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>
 
<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>
 
+
</syntaxhighlight>
  
  
 
<br />
 
<br />
 
====Change HTML Styles - CSS====
 
====Change HTML Styles - CSS====
This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":
+
Changing the style of an HTML element, is a variant of changing an HTML attribute:
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
+
document.getElementById("demo").style.fontSize = "35px";
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 84: Line 85:
 
<br />
 
<br />
 
====Hide HTML Elements====
 
====Hide HTML Elements====
This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":
+
Hiding HTML elements can be done by changing the display style:
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
+
document.getElementById("demo").style.display = "none";
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 92: Line 93:
 
<br />
 
<br />
 
====Show HTML Elements====
 
====Show HTML Elements====
This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":
+
Showing hidden HTML elements can also be done by changing the display style:
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
+
document.getElementById("demo").style.display = "block";
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 
<br />
 
<br />

Revision as of 13:53, 14 October 2019

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

JavaScript and Java are completely different languages, both in concept and design.

JavaScript is one of the 3 languages all web developers must learn:

  1. HTML to define the content of web pages
  2. CSS to specify the layout of web pages
  3. JavaScript to program the behavior of web pages

Web pages are not the only place where JavaScript is used. Many desktop and server programs use JavaScript. Node.js is the best known. Some databases, like MongoDB and CouchDB, also use JavaScript as their programming language.



What JavaScript can do

What JavaScript can do / What is used for / some of the most important methods:

  • JavaScript Can Change HTML Content
  • JavaScript Can Change HTML Attribute Values
  • JavaScript Can Change HTML Styles (CSS)
  • JavaScript Can Hide HTML Elements
  • JavaScript Can Show HTML Elements



Installation


IDE for JavaScript


Control flow statements


If statements


For Loops


While Loops


Methods


Some of the most important methods

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



Change HTML Content

This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":

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



Change HTML Attribute Values

In this example JavaScript changes the value of the src (source) attribute of an 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>



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";



Hide HTML Elements

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

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



Show HTML Elements

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

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