Get ID of the First and Last clicked DIV

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 65

Get ID of the First and Last clicked DIV

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>

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>