Linkedin search result so on the search page i want to only select the buttons with connect on them. the if statement doesnt seem to check if connect is the text content.
var buttons = document.getElementsByClassName('artdeco-button artdeco-button--2 artdeco-button--secondary ember-view')
for (var i = 0; i < buttons.length; i++) {
console.log(buttons[i].textContent)
if (buttons[i].textContent == 'Connect') {
console.log(i)
}
}
<button aria-label="Invite x to connect" id="ember82" class="artdeco-button artdeco-button--2 artdeco-button--secondary ember-view"><!---->
<span class="artdeco-button__text">
Connect
</span></button>
result Shouldnt the connect have an corresponding i?
You are performing an exact match on the string "Connect" which fails because the textContent in your example also includes a bunch of whitespace.
You can use String.prototype.trim() to remove it before testing.
var buttons = document.getElementsByClassName('artdeco-button artdeco-button--2 artdeco-button--secondary ember-view')
for (var i = 0; i < buttons.length; i++) {
console.log(buttons[i].textContent)
if (buttons[i].textContent.trim() == 'Connect') {
console.log(i)
}
}
<button aria-label="Invite x to connect" id="ember82" class="artdeco-button artdeco-button--2 artdeco-button--secondary ember-view"><!---->
<span class="artdeco-button__text">
Connect
</span></button>