Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Get the ID of element using the starting word of string present in it using jQuery

I know the starting word of the string present in the h2 element. How to find the ID of that element using contains?

The string present inside h2 is 'What Repairs Can Fix A P0500 Trouble Code?'. The sub string 'P0500' varies so it is not known. Is there a way to find the id using only the known words of the string.

const word = 'P0500';
let id = $(`h2:contains(${word})`).attr('id');

console.log(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<h2 id="heading2">What Repairs Can Fix A P0500 Trouble Code?</h2>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's working fine for me. I added a working snippet for the reference.

let id = $('h2:contains("test")').attr('id');

console.log(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h2 id="1">div1</h2>
<h2 id="2">This is a test, nested in div1</h2>
<h2 id="3">Nested in div1<h2>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!