1

I'm new to Javascript and working on a problem, it's about making a website that allows a user to input a bid on an item along with a bid id. When they input both they press the submit button and the bid/bid id (along with the date and time) will display in a textarea.

It should allow for multiple bids to be submitted and to display but currently I can only get one to display. Any help as to how I can get multiple bids to display would be appreciated. Thank you

var bids = new Array();
var bidders = new Array();
var bidTime = new Array();

function writeBid() {
     var historyText = " ";
     for (var i = 0; i < bids.length; i++) { 
     historyText = bidTime[i] + " " + bids[i] + " " + bidders[i] + "\n";
     document.bidForm.bidList.value = historyText;
     document.bidForm.highBid.value = bids[i];
     document.bidForm.bidId.value = " ";
     document.bidForm.bidAmount.value = " ";
     }
}
function addBid() {
     bidders.unshift(document.bidForm.bidId.value);
     bids.unshift(document.bidForm.bidAmount.value);
     var now = new Date();
     var hours = now.getHours();
     var minutes = now.getMinutes();
     var seconds = now.getSeconds();
     var timeText = hours + ":" + minutes + ":" + seconds;
     bidTime.unshift(timeText);
     writeBid();
 }
 function removeBid() {
     bids.shift();
     bidders.shift();
     bidTime.shift();
     writeBid();
 }
2
  • 1
    If bidList is the textarea, then use ...bidList.value += historyText - that is, use += instead of =. (Or, better, use += to add to your historyText variable and only set the ...bidList.value once after the loop.) Why are you displaying that sort of data in a textarea? Is it supposed to be editable? Commented Oct 14, 2016 at 1:14
  • the lab directions required it, not sure why. and when I use += it writes the previous entry again along with the new entry each time Commented Oct 14, 2016 at 1:22

1 Answer 1

1

as @nnnnnn said using += with your text variable works perfect:

JavaScript

var bids = [10, 20, 30];
var bidders = ['tim', 'sam', 'john'];
var bidTime = ['10/2/2013','12/5/213','14/1/2023'];

function writeBid() {
  var historyText = " ";
  for (var i = 0; i < bids.length; i++) { 
    historyText += bidTime[i] + " " + bids[i] + " " + bidders[i] + "\n";
    document.bidForm.highBid.value = bids[i];
    document.bidForm.bidId.value = " ";
    document.bidForm.bidAmount.value = " ";
  }
  document.bidForm.bidList.value = historyText;
}

HTML

<form name="bidForm" id="bidForm">
  <input type="text" name="bidId" id="bidId"/>
  <input type="text" name="bidAmount" id="bidAmount"/>
  <input type="text" name="highBid" id="highBid"/>
  <textarea name="bidList" id="bidList"></textarea>
</form>
Sign up to request clarification or add additional context in comments.

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.