2

How to get element by attribute "data-tid" with value of "43574" in Javascript? There is only one element with this value.

Please avoid using jQuery.

1

1 Answer 1

1

In recent browsers you can use

var elem = document.querySelector('[data-tid="43574"]')

but be warned it's not a fast selector.

To use a variable, do

var elem = document.querySelector('[data-tid="'+myValue+'"]')

or use

var elems = document.querySelectorAll('[data-tid]')

and loop over the found elements to check the value.

Sign up to request clarification or add additional context in comments.

7 Comments

May I suggest querySelector since it's only one element?
Ow, and how can I replace 43574 with variable?
It will probably be faster if you use querySelector instead of querySelectorAll, since it can stop once it finds the element.
@MackieeE That sets the attribute, it doesn't search for it.
String concatenation: '[data-tid="'+VARIABLE+'"]'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.