0

I am relatively new to JavaScript and I need help with a concept.

So, I have written up something such that, for example, inside the HTML file, I have a div with id called "GetID" which is where I want to display my string outputs.

Inside the JavaScript is such code that

for(looping through data) {
    if (Condition 1){
        document.getElementById("GetID").innerHTML = "Yellow"
    }
    else if (Condition 2){
        document.getElementById("GetID").innerHTML = "Red"
    }
}

My question is, how do I display the next condition besides the previous one instead of replacing it every time inside the div?

For example, if the loop runs, and first and second condition is Yellow, third is Red, I want it to display "Yellow, Yellow, Red" Right now, it keeps replacing it such that, Yellow, then Yellow, then Red - only 1 string output in same div ID, replacing the previous output

5 Answers 5

1

This is because you are reassigning innerHTML each time, and thus overwriting its contents.

Try innerHTML or innerText += "yellow" instead of just using '='. So:

for(looping through data) {
if (Condition 1){
    document.getElementById("GetID").innerHTML += "Yellow, "
}
else if (Condition 2){
    document.getElementById("GetID").innerHTML += "Red, "
}

}

This does leave you with a dangling comma at the end though.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I did not know of += functionality of displaying string for getting innerHTML. This helped
Certainly. That notation (+=) is just shorthand for old_string = old_string + new_string; You can use such notation with most other operators too, so something like: var x = 3; x *= 4; x; // 3 * 4, or 12;
1

I'd recommend using an array to hold your colors, if you want it all formatted nicely:

// Get element
var colorsElement = document.getElementById('GetID');
var colors = [];

// Clear element
colorsElement.innerHTML = '';

// Loop through and build array
for (loop) {
  if (condition1) {
    colors.push('Yellow');
  } else if (condition2) {
    colors.push('Red');
  }
}

// Display colors    
colorsElement.innerHTML = colors.join(', ');

This way you'll get "Yellow, Yellow, Red" just like you asked for. Editing the HTML directly on each iteration (as other answers recommend) is fine, but won't come out quite the same (for example, that will make it do "Yellow, Yellow, Red, " but there's no way for it to stop putting commas when it hits the last one). Using an array with join will format the string as you asked.

7 Comments

Why not just remove the comas? colors.join("")
Because then it would say "YellowYellowRed" which is not very readable. The OP asked for "Yellow, Yellow, Red", which is what my code prints.
Thank you for your help but how do you declare colorsElement.innerHTML = ' ' ; and colorsElement.innerHTML = colors.join (', '); When I input this in my code, the program do not run
You might be forgetting the line that says var colorsElement = document.getElementById('GetID'); at the top. You have to assign the variable before you use it.
I have the following written var colorsElement = document.getElementById('List'); var colors = []; colorsElement.innerHTML = ' ';
|
0

Just use += instead of = see fiddle: http://jsfiddle.net/23n76huL/14/

document.getElementById("GetID").innerHTML += "Yellow";

Comments

0

Another solution is to add new elements to you div.

The code here is based on the solution of Ben Blank

var arr = ["Yellow", "Red", "Blue"];

for(var i = 0; i < arr.length; i++) {
    var newSpan = document.createElement('span');
    newSpan.innerHTML = gallery.image[i].url;
    document.getElementById("GetID").appendChild(newSpan);
}

Comments

0

You can add the innerHTML value

document.getElementById("test").innerHTML += e.value;

my sample

http://jsfiddle.net/quwkh25y/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.