in Education by
I am working on a project where I need to use array values in the getElementById() in javascript. I tried various things, but the code isn't working. Please help me. I have an array of values like this: var examStateArr=["examState1","examState2","examState3","examState4","examState5","examState6","examState7"]; and i use it in getElementById() as: document.getElementById(examStateArr[str1-1]).value.innerHTML=xmlHttp.responseText But this doesnt work. The code works just fine when I hard code values like document.getElementById("examState1"); but not when i use the array. str1 is an integer that I pass from a jsp file below: <%for (int j = 1; j < 8; j++) {%> '> Please correct my mistake. Thank you in advance. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
var examStateArr = ["examState1", "examState2", "examState3", "examState4", "examState5", "examState6"]; for (var i = 0; i < examStateArr.length; i++) { document.getElementById(examStateArr[i]).innerHTML = xmlHttp.responseText } Mistake one done by you document.getElementById('').value.innerHTML -- is wrong document.getElementById('').innerHTML -- is correct Edit 2 make sure you call this function after DOM is loaded, try adding your script at the bottom

Related questions

0 votes
    I am attempting to solve a problem where if the elements in one array are squared, and all of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I'm trying to assemble and display image slices which have been preloaded into a two dimensional array, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I am trying to sort an array of objects by the name field but the ordering is wrong. The order I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    So basically this is what I want to do: There's a gap in the x axis between week 27 and week ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    There are loads of questions about the current date in JavaScript in general, but in my scenario, I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': ' ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm new to JavaScript. I have the following object: let obj1 = [{ 'id': 1, 'longString': ' ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...