JavaScript Snippets

JavaScript Details AJAX jQuery

Examples

Book/1/

JavaScript Can Change:

  • HTML content:

document.getElementById(“demo”).innerHTML = “Hello JavaScript”;

  • HTML attribute:

onclick=”document.getElementById(‘myImage’).src=’pic_bulboff.gif'”

  • CSS style:

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

  • hide/show HTML element::

onclick=”document.getElementById(‘demo’).style.display=’none'”

onclick=”document.getElementById(‘demo’).style.display=’block'”

Display Possibilities using:

  • innerHTML:

document.getElementById(“demo”).innerHTML = 5 + 6;

  • write():

document.write(5 + 6);

  • alert():

window.alert(5 + 6);

  • log():

console.log(5 + 6);

Include JavaScript

External: JavaScript File
<script type="text/javascript" src="javascript.js"></script>
Internal JavaScript code (prefered place is at the bottom of the <body> element):
<script> JavaScript code</script>

  • Variable shortcuts:
    (a) declaration and assignment in one statement:
    var price = 200;
    (b) many variables declaration and/or assignment in one statement:
    var person = "John Doe", carName = "Volvo", price = 200;
  • Array: the values (separated by commas) are assigned inside a pair of square brackets:
    var colors;
    colors ['white', 'black', ' custom '];
    Each item gets a number (starting with 0) called an index.
  • An expression results in a single value:
    (a) assign value to a variable:
    var color = 'yellow';
    (b) use two or more variables and operators:
    var area = 4 *3;

Books contents

JavaScript Details AJAX jQuery