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

99
Views
How to Loop Through Elements Ending with a number

I have a form with five input statements with the id ending with a sequential number like:

<input id="phone1" type="text" value="1111111111">
<input id="phone2" type="text" value="">
<input id="phone3" type="text" value="3333333333">
<input id="phone4" type="text" value="4444444444">
<input id="phone5" type="text" value="">

I attempted the following:

const nmbr = [1, 2, 3, 4, 5];
$.each(nmbr, function(index, value){
  var phn = $("#phone"+value)
  if !(phn.val()) {
     console.log('phone number missing');
   }
});

How would I step through these id's to get the value of the non-blank phone numbers.

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

0

You can use a class instead and then loop through the input and check if value is non empty

const inputs = document.querySelectorAll('.ipt');

for (const ipt of inputs) {
  if (ipt.value !== "") {
    console.log(ipt.value);
  }
}
<input class="ipt" id="phone1" type="text" value="1111111111">
<input class="ipt" id="phone2" type="text" value="">
<input class="ipt" id="phone3" type="text" value="3333333333">
<input class="ipt" id="phone4" type="text" value="4444444444">
<input class="ipt" id="phone5" type="text" value="">

about 4 years ago · Juan Pablo Isaza Report

0

In Below Code document.querySelectorAll('[id^="phone"]') will select all the ids with phone prefix.

    const data =  document.querySelectorAll('[id^="phone"]');
    console.log(data);
    
    
    for (const dt of data) {
      if (dt.value !== "") {
        console.log(dt.value);
      }
    }
    <input id="phone1" type="text" value="1111111111">
    <input id="phone2" type="text" value="">
    <input id="phone3" type="text" value="3333333333">
    <input id="phone4" type="text" value="4444444444">
    <input id="phone5" type="text" value="">

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!