If any element’s text is clicked once it is changed into “TEXT-1” otherwise if doublecliked into “TEXT-2”. So I have created a conditional with 2 different sentences according to the different output.
In the present JS there is a problem with e
which is not defined and I do not know how to fix it. Also I am not sure if the syntax of e.type === 'click'
and later on e.type === 'dblclick'
is correct.Thanks
JavaScript
x
22
22
1
if ( e.type === 'click')
2
{
3
document.querySelector('ul').addEventListener('click', onClick)
4
function onClick(e){
5
let val;
6
val = e.target.innerText = 'TEXT-1';
7
8
9
e.preventDefault();
10
11
console.log(val)
12
}} else if (e.type === 'dblclick'){
13
document.querySelector('ul').addEventListener('dblclick', onClick)
14
function onClick(e){
15
let val;
16
val = e.target.innerText = 'TEXT-2';
17
18
19
e.preventDefault();
20
21
console.log(val)
22
}}
JavaScript
1
11
11
1
<ul>
2
<li>
3
List Item 1
4
</li>
5
<li>
6
List Item 2
7
</li>
8
<li>
9
List Item 3
10
</li>
11
</ul>
Advertisement
Answer
You could try add multiple event listeners on your items.. In this example there is an unordered list with 3 items
JavaScript
1
12
12
1
const items = document.querySelectorAll('li');
2
3
items.forEach((e)=>{
4
e.addEventListener('click', ()=>{
5
e.textContent = "text1"
6
});
7
8
9
e.addEventListener('dblclick', ()=>{
10
e.textContent = "text2"
11
});
12
})
JavaScript
1
5
1
<ul>
2
<li>item 1</li>
3
<li>item 2</li>
4
<li>item 3</li>
5
</ul>