JavaScript
x
27
27
1
function edit(id) {
2
3
let name = document.querySelector('#name');
4
let price = document.querySelector('#price');
5
6
7
name.value = products[id].name;
8
price.value = products[id].price;
9
10
document.querySelector(".update").addEventListener('click', update('e',id));
11
12
}
13
14
15
function update(e,id) {
16
17
e.preventDefault;
18
19
let name = document.querySelector('#name');
20
let price = document.querySelector('#price');
21
22
products[id].name = name.value;
23
products[id].price = price.value;
24
25
showProduct();
26
27
}
I want to use preventDefault
in the function, but I could not fix that.
JavaScript
1
2
1
document.querySelector(".update").addEventListener('click', update('e',id));
2
In this line I send 'e'
for the event. In the update function I’m using like this:
JavaScript
1
2
1
e.preventDefault;
2
But its not working. How can I fix this problem. If you help me I will be glad.
Advertisement
Answer
you are sending the event as a string.
change this :
JavaScript
1
16
16
1
function edit(id) {
2
3
let name = document.querySelector('#name');
4
let price = document.querySelector('#price');
5
6
7
name.value = products[id].name;
8
price.value = products[id].price;
9
10
// change
11
document.querySelector(".update").addEventListener('click', e => {
12
update(e,id)
13
});
14
}
15
16