I’m currently trying to write an extension for chrome for a website to have some QoL upgrades, however I’m basically brand new when it comes to coding and having a lot of trouble trying to identify a specific element on the site. There’s more that needs to be done after that but at this point I’m just trying to make sure I can actually grab it and print something to console to verify it’s been pulled.
So this is my current code:
function replaceCheck() {
const elements = document.querySelectorAll(".wl-item.dropdown-item.added > a");
elements.forEach((element) => {
if (element.textContent.includes("Completed")) {
console.log("Successfully Identified");
}
});
}
With this I’m trying to grab the following element from a site:
Image of website element
The problem is is that nothing is printing to console when I run it, I need the code to include the "Completed" string as there are other possible strings depending on the option a user selects from a drop down.
Now for some additional clarification, I learned javascript self taught like 6 years ago now and never actually used it for anything really, so I basically have no idea what I’m doing at this point. This code was originally written up with the help of AI to get a starting point and tweaked it from there so this code could be completely incorrect in every way just trying to get an idea at this point.
2
Answers
you are trying to find the tag inside the a tag, try removing the "> a" because the ".wl-item.dropdown-item.added" is already it
Consider the following.
Using
textContent
might be more aggressive then you need. I usedinnerText
property.Versus:
It’s not clear, as you didn’t provide a complete example, how you are using or calling the function, so I am assuming it may be used in a logical statement.
Search will returns the index (position) of the first match or
-1
if not found. This example is case sensitive, so it must match "Completed" exacly.