JavaScript and HTML element manipulationHow do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?Why does HTML think “chucknorris” is a color?
What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?
Facing a paradox: Earnshaw's theorem in one dimension
Why are electrically insulating heatsinks so rare? Is it just cost?
What killed these X2 caps?
Modeling an IP Address
Is it canonical bit space?
Stopping power of mountain vs road bike
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
Doing something right before you need it - expression for this?
Can one be a co-translator of a book, if he does not know the language that the book is translated into?
I Accidentally Deleted a Stock Terminal Theme
How to take photos in burst mode, without vibration?
Should I tell management that I intend to leave due to bad software development practices?
Is "remove commented out code" correct English?
What's the difference between 'rename' and 'mv'?
I'm flying to France today and my passport expires in less than 2 months
How much of data wrangling is a data scientist's job?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
How can I make my BBEG immortal short of making them a Lich or Vampire?
How to say in German "enjoying home comforts"
Can a rocket refuel on Mars from water?
What to put in ESTA if staying in US for a few days before going on to Canada
SSH "lag" in LAN on some machines, mixed distros
JavaScript and HTML element manipulation
How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?Why does HTML think “chucknorris” is a color?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Project Concept: Creating an "exam maker", which can be accessed by a teacher to create and let a student be able to access it to take. Many features would be included but to keep it simple on the question at hand i wont be including all info.
Front End: List all questions in the database, using a php file, to a select field in HTML. When the item is selected add it to the test. Display the test, and assign scoring to each question.
My Actual Question/Help: My addq() function is supposed to, get the value of selected item, append it on the global testArray=[]; while the for loop iterates through each one to display them individually after each one is added.
The Problem: What mine is displaying in HTML... it keeps adding the arrays so the output is repeated over and over after each addq(). Please help fix it! -- the array needs to be outside the function so I can access it later and send it off to a php file.
<h4><center>Test</center></h4>
<ol id="test">
</ol>
<script>
var testArray= [];
function addq()
var addingquestion = document.getElementById('questionSelect').value;
var myArray = testArray.push(addingquestion);
var node = document.createElement("LI");
for(i=0;i<20;i++)
var textnode = document.createTextNode(testArray[i].toString());
node.appendChild(textnode);
document.getElementById("test").appendChild(node);
</script>
Example Output Issue Picture:
enter image description here
javascript php html for-loop
add a comment |
Project Concept: Creating an "exam maker", which can be accessed by a teacher to create and let a student be able to access it to take. Many features would be included but to keep it simple on the question at hand i wont be including all info.
Front End: List all questions in the database, using a php file, to a select field in HTML. When the item is selected add it to the test. Display the test, and assign scoring to each question.
My Actual Question/Help: My addq() function is supposed to, get the value of selected item, append it on the global testArray=[]; while the for loop iterates through each one to display them individually after each one is added.
The Problem: What mine is displaying in HTML... it keeps adding the arrays so the output is repeated over and over after each addq(). Please help fix it! -- the array needs to be outside the function so I can access it later and send it off to a php file.
<h4><center>Test</center></h4>
<ol id="test">
</ol>
<script>
var testArray= [];
function addq()
var addingquestion = document.getElementById('questionSelect').value;
var myArray = testArray.push(addingquestion);
var node = document.createElement("LI");
for(i=0;i<20;i++)
var textnode = document.createTextNode(testArray[i].toString());
node.appendChild(textnode);
document.getElementById("test").appendChild(node);
</script>
Example Output Issue Picture:
enter image description here
javascript php html for-loop
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45
add a comment |
Project Concept: Creating an "exam maker", which can be accessed by a teacher to create and let a student be able to access it to take. Many features would be included but to keep it simple on the question at hand i wont be including all info.
Front End: List all questions in the database, using a php file, to a select field in HTML. When the item is selected add it to the test. Display the test, and assign scoring to each question.
My Actual Question/Help: My addq() function is supposed to, get the value of selected item, append it on the global testArray=[]; while the for loop iterates through each one to display them individually after each one is added.
The Problem: What mine is displaying in HTML... it keeps adding the arrays so the output is repeated over and over after each addq(). Please help fix it! -- the array needs to be outside the function so I can access it later and send it off to a php file.
<h4><center>Test</center></h4>
<ol id="test">
</ol>
<script>
var testArray= [];
function addq()
var addingquestion = document.getElementById('questionSelect').value;
var myArray = testArray.push(addingquestion);
var node = document.createElement("LI");
for(i=0;i<20;i++)
var textnode = document.createTextNode(testArray[i].toString());
node.appendChild(textnode);
document.getElementById("test").appendChild(node);
</script>
Example Output Issue Picture:
enter image description here
javascript php html for-loop
Project Concept: Creating an "exam maker", which can be accessed by a teacher to create and let a student be able to access it to take. Many features would be included but to keep it simple on the question at hand i wont be including all info.
Front End: List all questions in the database, using a php file, to a select field in HTML. When the item is selected add it to the test. Display the test, and assign scoring to each question.
My Actual Question/Help: My addq() function is supposed to, get the value of selected item, append it on the global testArray=[]; while the for loop iterates through each one to display them individually after each one is added.
The Problem: What mine is displaying in HTML... it keeps adding the arrays so the output is repeated over and over after each addq(). Please help fix it! -- the array needs to be outside the function so I can access it later and send it off to a php file.
<h4><center>Test</center></h4>
<ol id="test">
</ol>
<script>
var testArray= [];
function addq()
var addingquestion = document.getElementById('questionSelect').value;
var myArray = testArray.push(addingquestion);
var node = document.createElement("LI");
for(i=0;i<20;i++)
var textnode = document.createTextNode(testArray[i].toString());
node.appendChild(textnode);
document.getElementById("test").appendChild(node);
</script>
Example Output Issue Picture:
enter image description here
javascript php html for-loop
javascript php html for-loop
asked Mar 7 at 23:56
ryan Kellyryan Kelly
82
82
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45
add a comment |
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45
add a comment |
2 Answers
2
active
oldest
votes
the problem is that you're appending the array every time to the node element. So, every time it will output the old values with the new ones
You don't have to make it as an array because it stacks without an array,
you just need to replace this :
var textnode = document.createTextNode(testArray[i].toString());
with this :
var textnode = document.createTextNode(addingquestion);
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
|
show 4 more comments
Here, you need to be creating you LI each time, it is an object.
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054707%2fjavascript-and-html-element-manipulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
the problem is that you're appending the array every time to the node element. So, every time it will output the old values with the new ones
You don't have to make it as an array because it stacks without an array,
you just need to replace this :
var textnode = document.createTextNode(testArray[i].toString());
with this :
var textnode = document.createTextNode(addingquestion);
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
|
show 4 more comments
the problem is that you're appending the array every time to the node element. So, every time it will output the old values with the new ones
You don't have to make it as an array because it stacks without an array,
you just need to replace this :
var textnode = document.createTextNode(testArray[i].toString());
with this :
var textnode = document.createTextNode(addingquestion);
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
|
show 4 more comments
the problem is that you're appending the array every time to the node element. So, every time it will output the old values with the new ones
You don't have to make it as an array because it stacks without an array,
you just need to replace this :
var textnode = document.createTextNode(testArray[i].toString());
with this :
var textnode = document.createTextNode(addingquestion);
the problem is that you're appending the array every time to the node element. So, every time it will output the old values with the new ones
You don't have to make it as an array because it stacks without an array,
you just need to replace this :
var textnode = document.createTextNode(testArray[i].toString());
with this :
var textnode = document.createTextNode(addingquestion);
answered Mar 8 at 0:36
xTrimyxTrimy
13112
13112
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
|
show 4 more comments
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
that is so much simpler.. woops! I was wondering if you can add a textbox to each question that can be filled out and data passed to a php file. For example ... itll list "question 1" [textbox1] "question2"[textbox2] ..etc. Unsure how to add a Textbox to each question submitted, Id appreciate any help with that!!
– ryan Kelly
Mar 8 at 0:40
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
It depends on what you're trying to do exactly
– xTrimy
Mar 8 at 0:48
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
so the goal is to .. Add Question to be displayed. (which was successfully done) .. give each question a text box to be a lotted scores. question1 = 10pts . etc like a normal exam. then take the information, question and the scores... to send them off. Is that enough info to get advice?
– ryan Kelly
Mar 8 at 0:51
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
I'm not very good at back-end, but I think I can help Here you will need to create a table in the database called "text" for example. Make a relation between it and the questions table, after adding the questions, you may redirect the user to a page where he can add a text for every question they've added... for example after adding 3 questions, get the questions from the database and connect them with the text table when saving.. question 1: <TEXTBOX> question 2: <TEXTBOX> question 3: <TEXTBOX> <SAVE>
– xTrimy
Mar 8 at 0:57
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
You must make sure that the 2 tables are connecting with each others through the ID the questions must have an id column and the texts must have a column named question_id for example hope that helps :)
– xTrimy
Mar 8 at 1:01
|
show 4 more comments
Here, you need to be creating you LI each time, it is an object.
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
add a comment |
Here, you need to be creating you LI each time, it is an object.
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
add a comment |
Here, you need to be creating you LI each time, it is an object.
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
Here, you need to be creating you LI each time, it is an object.
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
var testArray = [1,2,3,4,5,6,7,8,9,10];
function addq()
// REMOVED because not provided.
//let addingquestion = document.getElementById('questionSelect').value;
//let myArray = testArray.push(addingquestion);
let test = document.querySelector('#test');
testArray.forEach(t =>
// create new li for each item.
let li = document.createElement('li');
let textnode = document.createTextNode(t);
li.appendChild(textnode);
test.appendChild(li);
);
addq();
<h4>
<center>Test</center>
</h4>
<ol id="test">
</ol>
answered Mar 8 at 0:46
BibbertyBibberty
2,3021317
2,3021317
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
add a comment |
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
The problem here is that he is running the function every time the user choose a value without deleting the old values, so first time it's question 1, second time it's question1 + question1 & question2 and so on...
– xTrimy
Mar 8 at 1:25
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
Ah, gotcha. Thanks @MohamedAshraf
– Bibberty
Mar 8 at 1:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054707%2fjavascript-and-html-element-manipulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
because you make one li and keep adding the text to it.... You need a new li on every iteration.
– epascarello
Mar 7 at 23:58
if i move the "li" into the loop it will keep adding extra elements... where am i going wrong? @epascarello
– ryan Kelly
Mar 8 at 0:05
That is what I assumed you wanted....
– epascarello
Mar 8 at 2:45