AB Bryan
AB Bryan

Reputation: 73

How do I make keep these values from rewriting into the HTML?

I have a small piece of code that is writing some values into a textarea inside a form. The write whenever I click a button. However if I click the button more than once they will write over and over. I need to be able to click the button multiple times (for example if the user changes a value) and have the values that I am writing simply refresh rather than repeat. Here is what I have...

var endwallPanelLengths = [totalHeightInches];
    var i = 0;
    while (endwallPanelLengths[i] > eaveInches) 
    {
        endwallPanelLengths.push(endwallPanelLengths[i] - peakHeightDecrease);
        document.getElementById("test83").value += "4 - " + endwallPanelLengths[i] + "\n";
        i++;
    } 

When I click the button the first time the values are correct however they write again after everytome i click it?

Upvotes: 0

Views: 68

Answers (4)

Colleen
Colleen

Reputation: 25509

In this line:

    document.getElementById("test83").value += "4 - " + endwallPanelLengths[i] + "\n";

you're appending a value to test83. Presumably (since I can't see your html!!!) you just need to remove the + from the +=.

EDIT: in case you want to append all of the values in your loop, and nothing else, simply clear the value before you enter your loop and leave the +=

document.getElementById("test83").value = "";
var endwallPanelLengths = [totalHeightInches];
......

Upvotes: 2

bigbearzhu
bigbearzhu

Reputation: 2451

I think you want to do the whole iteration when the user change a value. Then you just need to reset the value of the textarea to empty before the loop. Something like this:

var endwallPanelLengths = [totalHeightInches];
var i = 0;
document.getElementById("test83").value = '';  //Reset the value
while (endwallPanelLengths[i] > eaveInches) {
    endwallPanelLengths.push(endwallPanelLengths[i] - peakHeightDecrease);
    document.getElementById("test83").value += "4 - " + endwallPanelLengths[i] + "\n";
    i++;
}

I have worked out a demo page. Hope that is what you are after.

Upvotes: 1

KING
KING

Reputation: 980

you need to implement a clickEventhandler that checks whether or not your texts exists.....

Upvotes: 0

Antoine
Antoine

Reputation: 2807

Try this:

var endwallPanelLengths = [totalHeightInches];
var i = 0;
document.getElementById("test83").value = '';
while (endwallPanelLengths[i] > eaveInches) 
{
    endwallPanelLengths.push(endwallPanelLengths[i] - peakHeightDecrease);
    document.getElementById("test83").value += "4 - " + endwallPanelLengths[i] + "\n";
    i++;
} 

I'm assuming that all the code is executed eacht time you click the button, am I right ?

Upvotes: 1

Related Questions