JavaScript Need index number of item in array on hover need of index number,why do we need index number,

I'm relatively new to JavaScript and I'm working on what should be a simple project. I'm stuck though and I'd love some opinions on ways to get a solution. I have a short products array like:

var products = [
   {
    name: "paper",
    price: 2.00,
    description: "White College-ruled Paper, 100 sheets",
    location: "Aisle 5"
   },
   {
    name: "pens",
    price: 5.00,
    description: "10 Pack, Black Ink Ball Point Pens"
    location: "Aisle 2"
   },
   {
    name: "paper clips",
    price: 0.50,
    description: "Silver Paper Clips, 100 count"
    location: "Aisle 6"
   }
  ]

I'm looping through this array using JS and printing the results to the page in a DIV with id of "output".

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

for (var i = 0; i < products.length; i += 1) {
  product = products[i];
  message += '<div class="col-md-4" id="prod-block">';
  message += '<p id="prod-description">' + product.name + ' ' + product.description + '</p>';
  message += '<h2 id="price">$' + product.price  + '</h2>';
  message += '</div>'
}
print(message);

All of this works just fine. I have my products on the page. Now, what I want is when the mouse hovers over any of the item divs, to show additional information (such as location) in a separate div. My question is - how do you identify the index number of the item that is being hovered over? As of now, the index number only exists in my for loop and I can't figure out how to access it in a different function.

Again, my knowledge is limited, so I'm not sure if writing the HTML in a for loop is even the best way to do this. I really appreciate any advice or criticism!!

Answer:1

Here's something that should help.

I updated your list to include an id attribute and I used that to assign a data attribute to the div that is being created. On hover it looks for the data-prodid and displays that in the additional section.

Fiddle

var products = [{
    id: 0,
    name: "paper",
    price: 2.00,
    description: "White College-ruled Paper, 100 sheets",
    location: "Aisle 5"
  }, {
    id: 1,
    name: "pens",
    price: 5.00,
    description: "10 Pack, Black Ink Ball Point Pens",
    location: "Aisle 2"
  }, {
    id: 2,
    name: "paper clips",
    price: 0.50,
    description: "Silver Paper Clips, 100 count",
    location: "Aisle 6"
  }],
  message = '';

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

for (var i = 0; i < products.length; i += 1) {
  product = products[i];
  message += '<div data-prodid="' + product.id + '" class="col-md-4" id="prod-block">';
  message += '<p id="prod-description">' + product.name + ' ' + product.description + '</p>';
  message += '<h2 id="price">$' + product.price + '</h2>';
  message += '</div>'
}
print(message);

$('.col-md-4').hover(function() {
  $('#additional').html($(this).data('prodid'));
});

Also

The javascript you posted has an error in your products variable and message was never declared.

Answer:2

try to add a tip tool as and when you create the div with the index number you want.

<div title="The index number">

Answer:3

There is something really wrong in your code: the abuse of id. Many elements will have the id prod-block, prod-description but an id has to be unique.

Doing this, you can easily detect which element is hovered just by checking the id. There is multiple techniques to do that, if you want to learn jQuery this is really easy to start this way.

Answer:4

If you use jQuery, you could use data() to define the data attribute of the DOM element, but you shoudl also use jQuery to add this element to the DOM.

$.each(products, function(i, product) {
    // Create DOM element
    var _ = $('<div class="col-md-4" id="prod-block">'
        + '<p id="prod-description">' + product.name + ' ' + product.description + '</p>'
        + '<h2 id="price">$' + product.price  + '</h2>'
        + '</div>');
    // Set product data
    _.data("product", product);
    // Add element to the DOM
    $("#output").append(_);
});
Answer:5

I want to place several elements of one class randomly on the page with Javascript / jQuery. My idea is to generate random values for margin-top, margin-left and z-index. The problem is that I need to ...

I want to place several elements of one class randomly on the page with Javascript / jQuery. My idea is to generate random values for margin-top, margin-left and z-index. The problem is that I need to ...

I'm taking help from the code given here http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/ to create a pie chart. But the problem is, if I replace some of those values with 0. Or ...

I'm taking help from the code given here http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/ to create a pie chart. But the problem is, if I replace some of those values with 0. Or ...

  1. creating chart with two y axis
  2. creating chart with excel
  3. creating chart with vba
  4. creating chart with timeline
  5. creating chart in ms excel
  6. creating chart in google sheets
  7. creating chart of accounts
  8. creating chart in word
  9. creating chart in powerpoint
  10. creating chart in excel 2016
  11. creating chart from excel data
  12. creating chart in illustrator
  13. creating chart in excel 2010
  14. creating chart in indesign
  15. creating chart in numbers
  16. creating chart in python
  17. creating chart in excel 2013
  18. creating chart in ms word
  19. creating chart in html
  20. creating chart in ms excel 2007

I have tried the following: <body id="myBody" onload = "setTimeout('a()', 5000)" / > Is this the correct method? The reason why I want to do this is because I have my entire website animating ...

I have tried the following: <body id="myBody" onload = "setTimeout('a()', 5000)" / > Is this the correct method? The reason why I want to do this is because I have my entire website animating ...

  1. load javascript after page load
  2. load javascript after document ready
  3. load javascript after html
  4. load javascript after jquery
  5. load javascript after 5 seconds
  6. load javascript after ajax call
  7. load javascript after another script
  8. load javascript after page render
  9. load javascript after button click
  10. load javascript after body
  11. load javascript after dom
  12. load javascript after click
  13. load javascript after php
  14. load javascript after ajax
  15. load javascript after some time
  16. javascript load after all scripts
  17. javascript load after 2 seconds
  18. javascript load after
  19. javascript load after everything
  20. javascript load after scroll

I am learning node.js, and would like to try building a blog on the platform. Do you know of any good tutorials that show you, step-by-step, how to build a blogging app on node? I have seen one by ...

I am learning node.js, and would like to try building a blog on the platform. Do you know of any good tutorials that show you, step-by-step, how to build a blogging app on node? I have seen one by ...