Help me with this😥 | Sololearn: Learn to code for FREE!

+10

Help me with this😥

I'm writing a code about a game, I have a div named card and it contains another div for img my img has data-tech="cool" and I write in js Console.log(firstCard.dataset.tech); Console.log(secondCard.dataset.tech); And it should type cool in console but all of my imgs are undefined.... What should I do now I'm stuck in it about 1 day

4/18/2021 12:35:29 PM

Nila (아미)

23 Answers

New Answer

+9

Unfortunately, your question is incomprehensible to me. I saw your codes under your profile and after that something like this should not make a problem. In add you do not want to show your attempt not even a snippet of that.

+5

Can you please share your attempt sister?

+5

YAQUB꧁NEW<INACTIVE>꧂ I cant🙁

+5

ηιℓα alternatively you can use getAttribute method to get value Use these method in window.onload or ensure that method call occur after this event

+5

This my code on PC but here changes a bit Plz help.... YAQUB꧁NEW<INACTIVE>꧂ Sandeep Kushwaha CamelBeatsSnake https://code.sololearn.com/WEgGu219SlUF/?ref=app

+5

ηιℓα Is this helpful? https://code.sololearn.com/WKA5vXtwV3L4/?ref=app

+4

Sandeep Kushwaha it's on my pc

+4

CamelBeatsSnake I didn't understand

+4

It's a memory game with Imgs that have data-tech

+4

You should add listener after window.onload occurs Like this 👇👇👇👇👇 https://code.sololearn.com/WKA5vXtwV3L4/?ref=app if first elements dataset doesn't match with second elements dataset then flipback it otherwise remove listener

+3

ηιℓα You're accessing this correctly by using the dataset, but make sure you wait for the DOM elements to load. You can do this by using an event listener for "DOMContentLoaded" or using the window load event handler (window.onload) to trigger your JavaScript. Console log out "firstCard" to see what it is. Is it the image? If you, you need to make sure you're selecting the right element. If you've waited for the DOM correctly it shouldn't be undefined, unless you're using an incorrect I'd or classname to select the image element.

+3

+3

ηιℓα then sorry

+3

ηιℓα Here's a working example. Please post your code attempt in future. You are much more likely to get the right help that way. https://code.sololearn.com/W8JK9WM3p4ky/?ref=app

+3

ηιℓα One problem with that code is you're not waiting for the DOM to load in your JavaScript. Please see my code example. You need to wait for that otherwise your HTML elements aren't available in JavaScript. So, to fix this problem, please wrap all your code inside this: window.addEventListener("DOMContentLoaded", () => { // All your other JS code can go here })

+3

CamelBeatsSnake Let me test it

+3

Again imgs r undefined and they stop CamelBeatsSnake I dont know why it shows undefined for the data-teches

+3

Sandeep Kushwaha it's useful I will mix it with my code

+2

I think data-tech is not a global html attribute, it's either you use an id or a className

+2

Sandeep Kushwaha I changed my code to this https://code.sololearn.com/WEgGu219SlUF/?ref=app But it should be sth like this at my pc https://code.sololearn.com/WCZd04FqRReC/?ref=app what should I do now 🤦‍♀️