I am developing a website that contains a series of products, each block contains a certain product, when hovering the mouse I need the name of this product to appear,
However, the product name is stored through an ‘DATA’ attribute. for example:
data-legend-item = “White T-Shirt”
I need to collect the value of this data attribute and make it appear every time I hover over it.
Remembering that they are a collection of blocks so I need to collect them from all data-legend-items on the page.
ps: notice that I made a script that only collects this value only from the first blockthat contains a data-legend-item
[
JavaScript
x
10
10
1
function dataTitleProduct(productItem) {
2
// collecting data-legend-item main attribute
3
4
var productItem = document.getElementById('item-title').getAttribute("data-legend-item");
5
// pulling the value of the data-legend-item attribute and inserting it in the html
6
document.querySelector('[data-legend-item]').innerHTML = productItem;
7
8
}
9
10
dataTitleProduct();
JavaScript
1
65
65
1
.products {
2
/* Div pai*/
3
max-width: 320px;
4
width: 100%;
5
}
6
7
8
/* Filhos recebendo distanciamento de 5 margin*/
9
10
.products .product-view {
11
margin: 5px auto;
12
}
13
14
15
/* */
16
17
.products .product-view {
18
max-width: 200px;
19
display: flex;
20
flex-flow: column wrap;
21
text-align: center;
22
margin: 0 auto;
23
}
24
25
.product-view .product {
26
height: 150px;
27
background-color: #ffffff;
28
box-shadow: 0 1px 3px #c7c7c7;
29
overflow: hidden;
30
white-space: nowrap;
31
text-overflow: ellipsis;
32
transition: all .3s ease;
33
position: relative;
34
}
35
36
.product-view .product:hover {
37
box-shadow: 0 7px 7px rgba(90, 90, 90, 0.2);
38
cursor: pointer;
39
content: '';
40
}
41
42
43
/* Titulo do Produto*/
44
45
.product-view .product [data-legend-item] {
46
display: block;
47
line-height: 220px;
48
position: relative;
49
font-size: 1.1rem;
50
color: #ffffff;
51
z-index: 1;
52
}
53
54
.product-view .product [data-legend-item]:before {
55
width: 100%;
56
height: 40px;
57
background-color: rgba(0, 0, 0, 0.5);
58
position: absolute;
59
top: 90px;
60
left: 0;
61
right: 0;
62
bottom: 0;
63
z-index: -1;
64
content: '';
65
}
JavaScript
1
31
31
1
<div class="products">
2
3
<div class="product-view">
4
5
<div id="item" class="product">
6
7
<div id="item-title" data-legend-item="T-shirt White"></div>
8
</div>
9
10
</div>
11
12
13
<div class="product-view">
14
15
<div id="item" class="product">
16
17
<div id="item-title" data-legend-item="Shoes"></div>
18
</div>
19
20
</div>
21
22
23
<div class="product-view">
24
25
<div id="item" class="product">
26
27
<div id="item-title" data-legend-item="Black T-shirt"></div>
28
</div>
29
30
</div>
31
</div>
]1
Advertisement
Answer
I prefer to hide and show using CSS put take a look at this this.
always use id
names only once in html file
JavaScript
1
11
11
1
document.querySelectorAll('.product-view').forEach(e => {
2
e.addEventListener('mouseover', event => {
3
let item_title = event.currentTarget.querySelector('.item-title');
4
item_title.innerText = item_title.dataset.legendItem;
5
});
6
7
e.addEventListener('mouseout', event => {
8
let item_title = event.currentTarget.querySelector('.item-title');
9
item_title.innerText = '';
10
})
11
})
JavaScript
1
65
65
1
.products {
2
/* Div pai*/
3
max-width: 320px;
4
width: 100%;
5
}
6
7
8
/* Filhos recebendo distanciamento de 5 margin*/
9
10
.products .product-view {
11
margin: 5px auto;
12
}
13
14
15
/* */
16
17
.products .product-view {
18
max-width: 200px;
19
display: flex;
20
flex-flow: column wrap;
21
text-align: center;
22
margin: 0 auto;
23
}
24
25
.product-view .product {
26
height: 150px;
27
background-color: #ffffff;
28
box-shadow: 0 1px 3px #c7c7c7;
29
overflow: hidden;
30
white-space: nowrap;
31
text-overflow: ellipsis;
32
transition: all .3s ease;
33
position: relative;
34
}
35
36
.product-view .product:hover {
37
box-shadow: 0 7px 7px rgba(90, 90, 90, 0.2);
38
cursor: pointer;
39
content: '';
40
}
41
42
43
/* Titulo do Produto*/
44
45
.product-view .product [data-legend-item] {
46
display: block;
47
line-height: 220px;
48
position: relative;
49
font-size: 1.1rem;
50
color: #ffffff;
51
z-index: 1;
52
}
53
54
.product-view .product [data-legend-item]:before {
55
width: 100%;
56
height: 40px;
57
background-color: rgba(0, 0, 0, 0.5);
58
position: absolute;
59
top: 90px;
60
left: 0;
61
right: 0;
62
bottom: 0;
63
z-index: -1;
64
content: '';
65
}
JavaScript
1
31
31
1
<div class="products">
2
3
<div class="product-view">
4
5
<div id="item" class="product">
6
7
<div class="item-title" data-legend-item="T-shirt White"></div>
8
</div>
9
10
</div>
11
12
13
<div class="product-view">
14
15
<div id="item" class="product">
16
17
<div class="item-title" data-legend-item="Shoes"></div>
18
</div>
19
20
</div>
21
22
23
<div class="product-view">
24
25
<div id="item" class="product">
26
27
<div class="item-title" data-legend-item="Black T-shirt"></div>
28
</div>
29
30
</div>
31
</div>