简体   繁体   中英

How to Find the user define email address exact match to a web list using Javascript

the user will provide a specific email address. in this case, varEmail = "admin@domain-name.onmicrosoft.com". now I have to check this on the web page list programmatically. I should be able to return the exact match of the user defined email address on the web page list.

Currently, I have these (2) email addresses on my trial environment. 在此处输入图像描述

I am also using this Javascript to get one of the email in the web page list.

(Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(name => name.textContent.match(/admin@domain-name.onmicrosoft.com/)))

how ever I am getting 2 output instead of the exact user define email address. Output: psi-admi@domain-name.onmicrosoft.com admin@domain-name.onmicrosoft.com

在此处输入图像描述

I am hoping that there is someone here will noticed my post and help me on this. thank you all in advanced!


Just an update, Here's what I've tried so far.

Array.from(document.getElementsByClassName("ms-List-surface")[2].getElementsByClassName("ms-List-cell")).filter(acct => acct.textContent === 'admin@domain-name.onmicrosoft.com');

Return Empty Empty Result

By the way, below is the screenshot of the page from where I need to get the Email/Account. Compliance New Content Search

I'm guessing the data in your HTML has some garbage in there, that's why exact match won't find what you need.

Try to clean the string up before comparing it, something like:

filter(name => name.textContent.trim().toLowerCase() === 'admin@domain-name.onmicrosoft.com')

that way you remove all the white spaces at the beginning and the end of the string, and convert it to lowercase (just in case someone messed up)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/tolowercase

This inquiry has been resolved. Thanks for the help of my Manager, who figured this all out. Again, I've just witnessed a cool pro dev's work ethic, from Identifying the where and how, the right search keywords, and so on.

So, yay, New feed, new learning!

here's the line of code he shared with me.

const userRow = document.evaluate("//div[text()='admin@domain-name.onmicrosoft.com']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.offsetParent;

He checked and Analyzed the result from the console. Since I am using 'textContent' or 'innerText', We went there to see its value. We found that both had an excess string attached to them ( eg usernameadmin@domain-name.onmicrosoft.com ) , that's why the above-suggested solutions didn't work (By the way, I would like to thank @Facundo & @tromgy).

From there, He finds ways to work on it and come up with a solution.

Please see link below for your reference:

How to get element by innerText

HTMLElement.offsetParent

Introduction to using XPath in JavaScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM