With the JavaScript object presented in this page you can draw arrow markers between the clicks coordinates inside a html element. The arrow markers are created with SVG.
The script contains a button that Enables /Disables the drawing action, and another button to delete the SVG arrow markers.

Code of the drawArrowSVG object

- Click on the code to select it.
<button id="btn_drawar">Enable Drawing</button> 
<button id="btn_delar">Delete Arrows</button>
<script>
//REPLACE 'container' WITH THE ID OF ELEMENT WHERE TO DRAW ARROWS
var elm_container = document.querySelector('#container');
 
//Draw arrows with SVG in the $parent element between the click coords
function drawArrowSVG(parent){
 //From: https://coursesweb.net/javascript/
  var me = this;
  var x, y = 0;  //contain the coordinates
  var drawarrow =0;  //if 2, draw the arrow
  var c_e1 ={};  // x,y coords for base line
  var c_e2 ={};  // x,y coords for arrow
  var container = parent;
  me.draw =-1;  //if 1 allow to draw the arrow

  // Get X and Y position of the elm (from: vishalsays.wordpress.com)
  function getXYpos(elm) {
    x = elm.offsetLeft;        // set x to elm’s offsetLeft
    y = elm.offsetTop;         // set y to elm’s offsetTop

    elm = elm.offsetParent;    // set elm to its offsetParent

    //use while loop to check if elm is null
    // if not then add current elm’s offsetLeft to x
    //offsetTop to y and set elm to its offsetParent
    while(elm != null) {
      x = parseInt(x) + parseInt(elm.offsetLeft);
      y = parseInt(y) + parseInt(elm.offsetTop);
      elm = elm.offsetParent;
    }

    // returns an object with "xp" (Left), "=yp" (Top) position
    return {'xp':x, 'yp':y};
  }

  // Get X, Y coords
  function getCoords(e){
    //if $draw is 1, get the coords and draw arrow
    if(me.draw ==1){
      var xy_pos = getXYpos(this);

      // if IE
      if(navigator.appVersion.indexOf("MSIE") != -1) {
        // in IE scrolling page affects mouse coordinates into an element
        // This gets the page element that will be used to add scrolling value to correct mouse coords
        var standardBody = (document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;

        x = event.clientX + standardBody.scrollLeft;
        y = event.clientY + standardBody.scrollTop;
      }
      else {
        x = e.pageX;
        y = e.pageY;
      }

      x = x - xy_pos['xp'];
      y = y - xy_pos['yp'];

      //set coords in c_e2 and c_e1; if drawarrow is 2 draw the arrow
      drawarrow++;
      if(drawarrow ==2){
        c_e2 = {x:x, y:y};
        drawarrow =0;
        drawArrow(c_e1, c_e2);
      }
      else c_e1 = {x:x, y:y};
    }
  }

  //append in #container SVG arrow with base in $c_e1 and the arrow in $c_e2 coords
  function drawArrow(c_e1, c_e2){
    var arrsvg = '<svg class="arrsvg" style="position:absolute; top:0; left:0; margin:0; width:99.8%; height:99.9%;"><defs><marker id="arrow" markerWidth="8" markerHeight="8" refx="3" refy="4" orient="auto"><path d="M1,1 L1,7 L7,4 L1,1" style="fill:red;" /></marker></defs><path d="M'+ c_e1.x +','+ c_e1.y +' L'+ c_e2.x +','+ c_e2.y +'" style="stroke:red; stroke-width: 2.3px; fill: none; marker-end: url('+ location.href.replace(/[#]*$/ig, '') +'#arrow);"/></svg>';
    container.insertAdjacentHTML('beforeend', arrsvg);  //add the arrow to the end in #container
  }

  //register click on $container to get the coords
  container.addEventListener('click', getCoords);
}

//draw arrow with SVG between the clicks coords in $elm_container
var drawAr = new drawArrowSVG(elm_container);

//register click on #btn_drawar to enable /disable drawing action
var btn_drawar = document.getElementById('btn_drawar');
if(btn_drawar) btn_drawar.addEventListener('click', function(e){
  drawAr.draw *=-1;
  e.target.style.background = (drawAr.draw ==1) ? '#f00' :'#dadafb';
  e.target.innerHTML = (drawAr.draw ==1) ? 'Disable Drawing' :'Enable Drawing';
});

//register click on #btn_delar to delete arrows
var btn_delar = document.getElementById('btn_delar');
if(btn_delar) btn_delar.addEventListener('click', function(e){
  var arrsvg = document.querySelectorAll('.arrsvg');
  for(var i=0; i<arrsvg.length; i++) arrsvg[i].outerHTML ='';
});
</script>

Usage

- Copy the code of the script above into your page.
- To the elm_container variable, replace "container" with the ID of the element where you want to draw arrows.
- By default, the arrow color is red, to change it, modify the "red" value in this code: style="fill:red;" in the drawArrow() function.

Example

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Draw arrow markers with clicks in html element</title>
<style>
#container {
position:relative;
width:150px;
height:185px;
margin:8px auto;
background:#fefefe;
border:2px dashed #111;
padding:3px;
}
#dv1, #dv2, #dv3 {
width:60px;
height:60px;
background:#88ee00;
border:1px solid blue;
}
#dv1 {
margin:0 0 0 80px;
}
#dv2 {
margin:0 0 0 40px;
}
#btn_drawar {
display:block;
margin:8px auto;
background:#dadafb;
}
</style>
</head>
<body>
You can draw arrow markers with SVG inside this element when the button "Enable Drawing" is pressed (has red color).
<div id="container">
<div id="dv1">DIV 1</div>
<div id="dv2">DIV 2</div>
<div id="dv3">DIV 3</div>
</div>
- Click on this button, to Enable the drawing action, then click inside the element above to draw arrows between clicks coords.<br>
If you click again on the button, the drawing action is disabled.
<button id="btn_drawar">Enable Drawing</button> 
<button id="btn_delar">Delete Arrows</button>
<script>
//REPLACE 'container' WITH THE ID OF ELEMENT WHERE TO DRAW ARROWS
var elm_container = document.querySelector('#container');
 
//Draw arrows with SVG in the $parent element between the click coords
function drawArrowSVG(parent){
 //From: https://coursesweb.net/javascript/
  var me = this;
  var x, y = 0;  //contain the coordinates
  var drawarrow =0;  //if 2, draw the arrow
  var c_e1 ={};  // x,y coords for base line
  var c_e2 ={};  // x,y coords for arrow
  var container = parent;
  me.draw =-1;  //if 1 allow to draw the arrow

  // Get X and Y position of the elm (from: vishalsays.wordpress.com)
  function getXYpos(elm) {
    x = elm.offsetLeft;        // set x to elm’s offsetLeft
    y = elm.offsetTop;         // set y to elm’s offsetTop

    elm = elm.offsetParent;    // set elm to its offsetParent

    //use while loop to check if elm is null
    // if not then add current elm’s offsetLeft to x
    //offsetTop to y and set elm to its offsetParent
    while(elm != null) {
      x = parseInt(x) + parseInt(elm.offsetLeft);
      y = parseInt(y) + parseInt(elm.offsetTop);
      elm = elm.offsetParent;
    }

    // returns an object with "xp" (Left), "=yp" (Top) position
    return {'xp':x, 'yp':y};
  }

  // Get X, Y coords
  function getCoords(e){
    //if $draw is 1, get the coords and draw arrow
    if(me.draw ==1){
      var xy_pos = getXYpos(this);

      // if IE
      if(navigator.appVersion.indexOf("MSIE") != -1) {
        // in IE scrolling page affects mouse coordinates into an element
        // This gets the page element that will be used to add scrolling value to correct mouse coords
        var standardBody = (document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;

        x = event.clientX + standardBody.scrollLeft;
        y = event.clientY + standardBody.scrollTop;
      }
      else {
        x = e.pageX;
        y = e.pageY;
      }

      x = x - xy_pos['xp'];
      y = y - xy_pos['yp'];

      //set coords in c_e2 and c_e1; if drawarrow is 2 draw the arrow
      drawarrow++;
      if(drawarrow ==2){
        c_e2 = {x:x, y:y};
        drawarrow =0;
        drawArrow(c_e1, c_e2);
      }
      else c_e1 = {x:x, y:y};
    }
  }

  //append in #container SVG arrow with base in $c_e1 and the arrow in $c_e2 coords
  function drawArrow(c_e1, c_e2){
    var arrsvg = '<svg class="arrsvg" style="position:absolute; top:0; left:0; margin:0; width:99.8%; height:99.9%;"><defs><marker id="arrow" markerWidth="8" markerHeight="8" refx="3" refy="4" orient="auto"><path d="M1,1 L1,7 L7,4 L1,1" style="fill:red;" /></marker></defs><path d="M'+ c_e1.x +','+ c_e1.y +' L'+ c_e2.x +','+ c_e2.y +'" style="stroke:red; stroke-width: 2.3px; fill: none; marker-end: url('+ location.href.replace(/[#]*$/ig, '') +'#arrow);"/></svg>';
    container.insertAdjacentHTML('beforeend', arrsvg);  //add the arrow to the end in #container
  }

  //register click on $container to get the coords
  container.addEventListener('click', getCoords);
}

//draw arrow with SVG between the clicks coords in $elm_container
var drawAr = new drawArrowSVG(elm_container);

//register click on #btn_drawar to enable /disable drawing action
var btn_drawar = document.getElementById('btn_drawar');
if(btn_drawar) btn_drawar.addEventListener('click', function(e){
  drawAr.draw *=-1;
  e.target.style.background = (drawAr.draw ==1) ? '#f00' :'#dadafb';
  e.target.innerHTML = (drawAr.draw ==1) ? 'Disable Drawing' :'Enable Drawing';
});

//register click on #btn_delar to delete arrows
var btn_delar = document.getElementById('btn_delar');
if(btn_delar) btn_delar.addEventListener('click', function(e){
  var arrsvg = document.querySelectorAll('.arrsvg');
  for(var i=0; i<arrsvg.length; i++) arrsvg[i].outerHTML ='';
});
</script>
</body>
</html>
- Demo:
You can draw arrow markers with SVG inside this element when the button "Enable Drawing" is pressed (has red color).
DIV 1
DIV 2
DIV 3
- Click on this button, to Enable the drawing action, then click inside the element above to draw arrows between clicks coords.
If you click again on the button, the drawing action is Disabled.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Draw arrow markers with clicks in html element

Last accessed pages

  1. Common PHP Errors and Solutions (6867)
  2. Get visitor IP in PHP (842)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (43359)
  4. SSEP - Site Search Engine PHP-Ajax (7095)
  5. PHP Method Chaining (1706)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (697)
  2. Read Excel file data in PHP - PhpExcelReader (625)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (394)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (347)
  5. PHP getElementById and getElementsByTagName (311)