How do I use the querySelector()?

Hello :slight_smile:

I have the following HTML script above that I added as a screenshot. I now have to create a command which stores the (“Raspberry Pi music machine”) in a variable called selection 2, using the querySelector(). My answer would have been:

let selection2 = document.querySelector("#projects-page .thumbnail.p2.selected h2");

but it’s apparently too specific. Does somebody know the answer?:confused:

Explain in words what this selector is targeting.

Actually, scratch that, I misread your image. It’s always better to post actual code in here than images of code. The images can be hard to read and it doesn’t allow us to copy/paste for our own testing.

2 Likes

And I would follow up and ask what the goal is here. That selector seems to be perfect for extracting the text you’re looking for, but without context to your goal, we have to go off limited information.

Is this for a challenge? Personal project? Work?

2 Likes

Hello :slight_smile: Thank you for your quick answer. I am currently taking a beginners class at university and we were given a portfolio HTML document with the code that I gave you above.
The question that was given to me now is : Using querySelector(), what command selects the element that has the title of the project description (“Raspberry Pi music machine”) and stores it into variable named selection2 ?

I am submitting my answer online which means I only get a wrong or right on my answer. When I asked the teacher why my answer was wrong he mentioned that h2 is not needed in that specific case and that .p2 is also too specific because the selector wouldn’t work anymore if it was called p1 for example. I still don’t know the answer though :smiley:

1 Like

I think we would need to know the exact requirements for this problem. As @marcusparsons said, the selector list you used seems to do what you want it to do. But if there are other requirements that we don’t know about that make your solution invalid then we can’t really comment on that.

2 Likes

Hello :slight_smile: I only have the task that I posted under Marcus post x It’s part of an university online course so I thought I’m just doing something wrong at this point

1 Like

OK, then we are all confused as to why it won’t accept your apparently correct solution :slightly_smiling_face:

Well, you have to have something uniquely identifiable about the element in order to determine if it contains the “title of the project description”. So if your instructor is saying that the title of the project description is not always going to be in an h2 element and is not always going to be nested in a .p2 element then what one unique characteristic can be used to determine how to find the title of the project?

Until you know that I don’t see how you can write an accurate selector for this problem.

1 Like

Well at least it’s comforting to know that my answer doesn’t seem to be the problem :smiley: Thank you for helping out, I guess I will just have to reach out to my professor again

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.