JavaScript Find out which element was clicked from the list of elements find which element has focus,find which element is scrolling,

I have a few divs that share the same class. I have a dropdown that changes the background color of one of those divs when selected(option 2 changes div #2).
My question is how can I change the dropdown option when clicking a certain div?
I have the logic behind changing the actual option and the div clicks, but I can't figure out how to find exactly which of the divs was clicked(the divs are dynamically created, so they don't have Ids, only class names).
Is there any way to check what div was clicked relatively to the entire list of divs with the same class? Thanks.
Example code:

<select class="size form-control" id="size" name="size">
<option value="1">first div</option>
<option value="2">second div</option>
<option value="4">third div..</option>
</select>

<div class="collection">random</div>
<div class="collection">text</div>
<div class="collection">inside</div>
<div class="collection">here</div>

Edit:
What I have:
The select can change the color of a div depending on choice.
Clicking a div changes it's own color.
What I need:
Clicking a div also changes the option in the select. I hope it's a bit clearer now

Answer:1

With jQuery You can get the index of the clicked div by using .index()

$('div.collection').click(function () {
    var index = $('div.collection').index($(this));
    console.log(index);

    $('select').val(index + 1);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="collection">Div1</div>
<div class="collection">Div2</div>
<div class="collection">Div3</div>
<div class="collection">Div4</div>

<select class="size form-control" id="size" name="size">
    <option value="1">first div</option>
    <option value="2">second div</option>
    <option value="4">third div..</option>
</select>
Answer:2

You can use jQuery for this, It's very simple:

<select class="size form-control" id="size" name="size">
  <option value="1">first div</option>
  <option value="2">second div</option>
  <option value="4">third div..</option>
</select>

<div class="collection">Div1</div>
<div class="collection">Div2</div>
<div class="collection">Div3</div>
<div class="collection">Div4</div>

jQuery:

$('div.collection').click(function(){
var index = $('div.collection').index($(this)) + 1;
  $('select').val(index);
});

Example: http://codepen.io/ilanus/pen/zBgvJr

Answer:3

Wrap the div around a parent div and use index() to get the position of div

$('.collection').click(function(){
  var position = $(this).index()+1;
  console.log(position);
  $('.size').val(position);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="size form-control" id="size" name="size">
<option value="1">first div</option>
<option value="2">second div</option>
<option value="3">third div..</option>
  <option value="4">Fourth div..</option>
</select>
<div>
  <div class="collection">random</div>
  <div class="collection">text</div>
  <div class="collection">inside</div>
  <div class="collection">here</div>
</div>
Answer:4

I am currently creating a webpage using HTML/CSS and jQuery/Basic JS. I'm using a jQuery code to show a GIF image while the website is loading $(window).load(function() { $(".loading").fadeOut(...

I am currently creating a webpage using HTML/CSS and jQuery/Basic JS. I'm using a jQuery code to show a GIF image while the website is loading $(window).load(function() { $(".loading").fadeOut(...

  1. website loading page design
  2. website loading page
  3. website page loading speed test
  4. website page loading speed
  5. website page loading animation
  6. website page loading time
  7. website page loading test
  8. website page loading slow
  9. website loading blank page
  10. create website loading page
  11. website stuck loading page
  12. website not loading blank page

I have a basic bootstrap modal to log in, which doesn't close itself when I hit submit. I solved it using this simple jquery: $('#myModal').modal('hide'); But I also want the modal to fade out ...

I have a basic bootstrap modal to log in, which doesn't close itself when I hit submit. I solved it using this simple jquery: $('#myModal').modal('hide'); But I also want the modal to fade out ...

  1. hide bootstrap modal jquery
  2. hide bootstrap modal using javascript
  3. hide bootstrap modal after 3 seconds
  4. hide bootstrap modal on page load
  5. hide bootstrap modal angular
  6. hide bootstrap modal on button click
  7. hide bootstrap modal using jquery
  8. hide bootstrap modal popup using jquery
  9. hide bootstrap modal
  10. hide bootstrap modal on ajax success
  11. hide bootstrap modal on click
  12. hide bootstrap modal backdrop
  13. hide bootstrap modal js
  14. hide bootstrap modal in angular 4
  15. hide bootstrap modal from code behind c#
  16. hide bootstrap modal after submit
  17. hide bootstrap modal react
  18. hide bootstrap modal in c#
  19. hide bootstrap modal event
  20. hide bootstrap modal angularjs

How to convert string to timestamp and date in JS. here am using Date.parse(), but its not working in IE and FF. My code is.. in chrome its working fine. var str = "05-Sep-2013 01:05:15 PM "; ...

How to convert string to timestamp and date in JS. here am using Date.parse(), but its not working in IE and FF. My code is.. in chrome its working fine. var str = "05-Sep-2013 01:05:15 PM "; ...

  1. date.parse not working in ie
  2. date.parse not working in safari
  3. date.parse not working
  4. date.parse not working in ie11
  5. date.parse not working in firefox
  6. date.parse not working in mozilla
  7. date.parse not working in chrome
  8. date.parse not working in internet explorer
  9. date.parse not working jquery
  10. date.parse not working java
  11. date.parse javascript not working
  12. tableau date parse not working
  13. date.parse in javascript not working in ie
  14. javascript date.parse not working in firefox

A bit of a newbie question but what is the best way to get a list with full name path of all JS files loaded on my website? To clarify: I have a page www.mypage.com - I want to get a list of all the ...

A bit of a newbie question but what is the best way to get a list with full name path of all JS files loaded on my website? To clarify: I have a page www.mypage.com - I want to get a list of all the ...

  1. php list loaded files
  2. gdb list loaded files
  3. ruby list loaded files
  4. vim list loaded files
  5. emacs list loaded files