0

I'm trying to find a JavaScript element ID and I only know the title.

var x = document.querySelectorAll('[title="My Workforce"]');

var id = x.getAttribute('id');

I don't know how to write the title so that it is any approximation of "My Workforce". I have tried using "*" (as in "*My Workforce*") but it didn't work. querySelectorAll() looks for an exact match.

Paul Roub
  • 35,100
  • 27
  • 72
  • 83
  • In the above, I have tried to use the asterix wildcard, like this "\*My Workforce\*", but it didn't help – Claudiu Cojocaru Feb 24 '16 at 15:14
  • 2
    If by "any approximation", you're saying you want to search for the substring in the value, use `title*="My Workforce"` –  Feb 24 '16 at 15:18
  • 1
    squint is correct. BTW, you probably want `querySelector()`, otherwise you'll need to use `x[0]` once you get the desired results from `querySelectorAll()` - it returns a *list* of elements. – Paul Roub Feb 24 '16 at 15:20

1 Answers1

0

You can use querySelector() instead. This allows the use of wildcards, like so:

https://jsfiddle.net/xtukey8o/

document.querySelector('[title*="My Workforce"]')
MortenMoulder
  • 5,021
  • 6
  • 44
  • 89