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

200
Views
Pass dynamic variables with PHP ajax to Javascript

So I want to pass a variable like : childage_error1 childage_error2 childage_error3 etc. And I have this code right here:

for (var i = 0; i < gyermekek; i++) {
      document.getElementById('gyermekkor_error' + (i + 1) + '').innerHTML = response.window['gyermekkor_error'+i+1]
    }

If I put manually "gyermekkor_error1" it works but it wont work in a loop. It works in getElementById but not in the end.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

response.window['gyermekkor_error'+i+1]

You are adding a string to a number plus a number. Basic order of operations left to right. The code does not assume you meant to add i to 1 first.

So you are getting

'gyermekkor_error01'
'gyermekkor_error11'
'gyermekkor_error21'
'gyermekkor_error31'

You need to surround the addition part with parenthesis

response.window['gyermekkor_error' + (i+1)]

Or just start your loop off at one so you do not need the addition step.

for (var i = 1; i <= gyermekek; i++) {
  var key = 'gyermekkor_error' + i;
  document.getElementById(key).innerHTML = response.window[key];
}
about 4 years ago · Santiago Gelvez 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!