Javascript Course

The code presented below can be used to highlight images on page when the user clicks on them then, if the user clicks again on that image, the highlight style is removed.
- The script adds CSS style with JavaScript: "padding", "background", and "border" to the image clicked by the user, when the user clicks again on that image, the highlight effect is removed.

Code of the script:

// set image properties, for Highlight effect added when click
var imgProp = {
 'padding': '3px',
 'backgroundColor': '#eded01',
 'borderSize': '1ps',
 'borderStyle': 'dashed',
 'borderColor': '#0001fe'

// function to highlight IMGs on click - from:
function highlightImg() {
 // gets all <img> tags, and their number
 var allimgs = document.getElementsByTagName('img');
 var nrallimgs = allimgs.length;

 // traverses the <img> elements, and register onclick to each one
 // else, apply the properties defined in $imgProp
 for(i=0; i<nrallimgs; i++) {
 allimgs[i].onclick=function() {
 // if borderStyle is already applied, anulates the 'padding', 'background' and 'border' properties
 if( == imgProp.borderStyle) { = 'auto'; = 'none'; = 'none';
 else { = imgProp.padding; = imgProp.backgroundColor; = imgProp.borderSize; = imgProp.borderStyle; = imgProp.borderColor;

// calls the highlightImg() function to apply the effect
- In the imgProp object you can define the highlight style: "padding", "background", and "border" properties.
- The effect is for all the <img> tags in the page; if you want the highlight effect to be applied only to the images added into a specified HTML element, replace this code:
var allimgs = document.getElementsByTagName('img');
With this line of code ('idelm' is the ID of the element in which the images are included).
var allimgs = document.getElementById('idelm').getElementsByTagName('img');
- The JavaScript script must be added at the end of the HTML document, before the ending </body> tag.

• Demo, click on these images.
Robot     Love     Angel

Daily Test with Code Example

What attribute makes the input text box to not be modified by the user?
checked="checked" readonly="readonly" disabled="disabled"
<input type="text" value="fixed-value" readonly="readonly" name="a_name" />
What CSS property allows you to create rounded corners in your webpage design?
background-size border-size border-radius
.class {
  border:2px solid blue;
What instruction displays a confirmation dialog box to the viewer, who must then click OK or Cancel to proceed?
indexOf() confirm() prompt()
var ques = window.confirm("The result of 0+0 is 0?");
if (ques) alert("Corect");
else alert("Incorrect");
Indicate the PHP function that returns the lowest number of the parameter values.
floor() ceil() min()
$min_nr = min(12, 8, 25, 13);
echo $min_nr;        // 8
Highlight Images on click

Last accessed pages

  1. Accessing objects in different Timeline (1357)
  2. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (50086)
  3. Remove / Get duplicate array values - Reset array keys in PHP (11404)
  4. Get and change IFrame content through a JavaScript script created in another IFrame (11835)
  5. PHP MySQL - INSERT INTO (13234)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (84)
  2. Read Excel file data in PHP - PhpExcelReader (65)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (55)
  4. SHA1 Encrypt data in JavaScript (54)
  5. querySelector and querySelectorAll (48)