Environment: Just JavaScript
Is there a way to get an element that contains partial text?
<h1 id="test_123_abc">hello world</h1>
In this example, can I get the element if all I had was the test_123
part?
Environment: Just JavaScript
Is there a way to get an element that contains partial text?
<h1 id="test_123_abc">hello world</h1>
In this example, can I get the element if all I had was the test_123
part?
Since you can't use Jquery, querySelectorAll
is a descent way to go
var matchedEle = document.querySelectorAll("[id*='test_123']")
querySelectorAll with starts with
var elems = document.querySelectorAll("[id^='test_123']")
console.log(elems.length);
<h1 id="test_123_abc">hello world</h1>
<h1 id="test_123_def">hello world</h1>
<h1 id="test_123_ghi">hello world</h1>
You can achieve it (without using jQuery) by using querySelectorAll.
var el = document.querySelectorAll("[id*='test_123']");
You can get a clear example of it by going through the following link:
Find all elements whose id begins with a common string