/    Sign up×
Community /Pin to ProfileBookmark

Touch Listener doesn’t work for the rest of Elements

I am working on my portfolio and I have an issue with the touch event.

It works fine only the first element when classes are selected with querySelector but when I want to make the code work for other elements and I select classes with querySelectorAll the holde code stops working.

Here is the HTML: https://github.com/CodyHiII/Second-Portfolio/blob/master/design.html

Here is the JS: (The code that works)

[code]const card = document.querySelector(‘.t-card’);
const hidden = document.querySelector(‘.hidden-img’);

card.addEventListener(‘touchstart’, ()=> {
hidden.classList.toggle(‘tap-show’);
});

const cardB = document.querySelector(‘.card-container’);
const businessCard = document.querySelector(‘.card’);

cardB.addEventListener(‘touchstart’, ()=> {
businessCard.classList.toggle(‘tap-turn’);
});[/code]

to post a comment
HTMLJavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 19.2019 — Have a close look at my demo here:

https://www.webdeveloper.com/d/385141-hover-on-mobile

It shows you how to apply that effect on multiple images.
Copy linkTweet thisAlerts:
@codyhillauthorJul 19.2019 — @Sempervivum#1606480 Hi @Sempervivum#1606480, I have tried that, I even copied and pasted your code. I tried with this. I tried with forEach I tried with e => and () =>.

This code is just like yours but with the exception, you have this.classList.toggle and I have a const.
const card = document.querySelector('.t-card');
const hidden = document.querySelector('.hidden-img');

card.addEventListener('touchstart', (e)=> {
event.preventDefault();
hidden.classList.toggle('tap-show');

});

t-card is a container for the card and hidden-img is an image on top and on click hidden-img gets opacity 0. I did not change my names as I made changes to HTML.
Copy linkTweet thisAlerts:
@SempervivumJul 19.2019 — you have this.classList.toggle and I have a const.[/quote]`const</C> is not significant in this context but <C>this</C> is. Of course my code is not ready for copy&amp;paste, it needs to be adjusted to your environment. A rough approch shoud be this:
<CODE>const cards = document.querySelectorAll('.t-card');
cards.forEach(function(item, idx) {
item.addEventListener('touchstart', function() {
event.preventDefault();
this.classList.toggle('tap-show');
}); &lt;br/&gt;
});
</CODE>The rest can be done by CSS. Note what MDN says:<QUOTE>An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords.[/quote]</QUOTE>where <C>
this`
is significant for your task.
Copy linkTweet thisAlerts:
@codyhillauthorJul 19.2019 — @Sempervivum#1606496, Of course, I adjusted your code. I didn't just copy it. I figured the problem out. Thx!
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...