Page 1 of 1

Get ID of the First and Last clicked DIV

Posted: 05 Dec 2014, 14:21
by Marius
Hi,
How can i st into a variable, for example an Array with two elements, the ID of the first and the last clicked Div which is into a parent DIV.
For example:

Code: Select all

<div id="parent">
  <div id="id1">https://coursesweb.net/</div>
  <div id="id2">http://www.marplo.net/</div>
  <div id="id3">http://php.net/</div>
  <div id="id4">http://jquery.com/</div>
  ...
</div>
<script>
var fl_id = ['id1', 'id2'];    // here the id of the first / last clicked div
</script>

Get ID of the First and Last clicked DIV

Posted: 05 Dec 2014, 14:43
by Admin
Hi,
I think you can use this code.

Code: Select all

<div id="parent">
  <div id="id1">https://coursesweb.net/</div>
  <div id="id2">http://www.marplo.net/</div>
  <div id="id3">http://php.net/</div>
  <div id="id4">http://jquery.com/</div>
  ...
</div>
<script>
var fl_id = ['', ''];    // here the id of the first / last clicked div
var divs = document.getElementById('parent').querySelectorAll('div');
for(var i=0; i<divs.length; i++) {
  divs[i].addEventListener('click', function(){
    if(this.id != '' && this.id != undefined) {
      if(fl_id[0] == '') fl_id[0] = this.id;
      fl_id[1] = this.id;
  // console.log(fl_id);    // debug
    }
  }, false);
}
</script>