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:
<script type="text/javascript">
// 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: https://coursesweb.net/
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(this.style.borderStyle == imgProp.borderStyle) {
        this.style.padding = 'auto';
        this.style.background = 'none';
        this.style.border = 'none';
      }
      else {
        this.style.padding = imgProp.padding;
        this.style.backgroundColor = imgProp.backgroundColor;
        this.style.borderSize = imgProp.borderSize;
        this.style.borderStyle = imgProp.borderStyle;
        this.style.borderColor = imgProp.borderColor;
      }
    }
  }
}

// calls the highlightImg() function to apply the effect
highlightImg();
</script>
- 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

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Highlight Images on click

Last accessed pages

  1. Get Duration of Audio /Video file before Upload (5913)
  2. Follow the mouse cursor with a DIV inside a Parent (2703)
  3. Register and show online users and visitors (23278)
  4. Node.js Move and Copy Directory (1127)
  5. SHA1 Encrypt data in JavaScript (8256)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (390)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (332)
  3. Register and show online users and visitors (302)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (234)
  5. Insert, Select and Update NULL value in MySQL (223)