-1

I have been working on website construction for the whole day. Since I am pretty new to Javascript/HTML, I always get stuck. Today I coded this file and the code worked properly when I finished the first two javascript functions, but all three functions became "undefined" in the console when I finished the third function. They do not do anything when I open this file in my browser. Here is my code:`

<html>
<head>
<title> Broker Page -- Stock Operating System </title>
<link rel="stylesheet" type="text/css" href="styleSheet.css" />
<h1> Welcome to Broker Page! </h1>

<script type="text/javascript"> 
function executeStock() {
     var stockPrice = Math.random() * 10;
     var inputStr = document.getElementById('stockName').value;
     if (inputStr != "") {
         if (inputStr.length != 6)
             alert('Please enter 6 digits for the stock ID!');
         else if (inputStr >= 0)
             document.write("The stock price for the stock you are searching for is " + stockPrice.toPrecision(2));
         else 
             document.write("<h1> <font face="Calibri"> The stock ID you entered is invalid! It must only contain digits! </font> </h1>");
     }
}

function buyStock() {
     var stockID = document.getElementById('buyingStock').value;
     var stockShares = document.getElementById('stockShare').value;
     if (stockID.length != 6) {
          alert("The stock ID you entered must be six digits!");
          return;
     }
     if (stockShares > 0) {
          alert("You succeeded!");
     } else 
          document.write("<h1>The stock share you entered is invalid. Please try again!</h1>");
}

function sellStock() {
     var stockIDToSell = document.getElementById('sellOne').value;
     var sellSh = document.getElementById('sellShare').value;
     if (stockIDToSell.length != 6) {
          alert("The stock ID you entered must be six digits!");
          return;
     }
     if (sellSh > 0) {
          alert("You sold " + sellSh + " share of stock with ID: " + stockIDToSell);
     } else 
          document.write("<h1>The stock share you entered is invalid. Please try again!</h1>");
}

</script>

</head>
<body>
<div align="center">
<form method="post" onsubmit="executeStock();" >
<font face="Calibri" color=#880000> Search Stock </font>
<input type="text" id="stockName" size="13" />
<input type="submit" id="SubmitSearch" value="go" style="width:40px;height:30px;font-size:16" />
</form>
</div>
<br> </br> 
<div align="center">
<form method="post" onsubmit='buyStock();'>
<font face="Calibri" color=#880000 size=12> Buy Stocks </font> <br> </br>
<font face="calibri" color=#000000> Enter the stock ID: </font>
<input type="text" value="" id="buyingStock" />
<br> </br> 
<font face="calibri" color=#880000> Enter share: </font>
<input type="text" value="" id="stockShare" />
<input type="submit" value="Buy!" />

</form>

<form method="post" onsubmit='sellStock();'>
<font face="Calibri" color=#008800 size=12> Sell Stocks </font> 
<br> </br>
<font face="calibri" color=#000000> Enter the stock ID: </font>
<input type="text" value="" id="sellOne" />
<br> </br> 
<font face="calibri" color=#880000> Enter share: </font>
<input type="text" value="" id="sellShare" />
<input type="submit" value="Sell!" />

</form>

</div>
<div align="center">
<p> <font face="Calibri"> Your client information and recent transactions will be loaded here. </font> </p>
</div>

</body>
</html>

` Can anyone help me with this issue? This is definitely NOT a duplicate question. Thanks.

922lmh922
  • 11
  • 1
  • 3
  • you should not use `document.write`, use [`Element.innerHTML`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) instead – Hacketo Mar 26 '15 at 08:23
  • it is because of the document.write() - if it is called after document ready it will replace the document – Arun P Johny Mar 26 '15 at 08:24
  • start using developper tools or firebug. it's really hard to find out where the cause is without debugger even if you were MASTER of javascript.face="Calibri" making an error. – suish Mar 26 '15 at 08:26
  • http://jsfiddle.net/arunpjohny/p6kh22yu/1/ – Arun P Johny Mar 26 '15 at 08:28
  • possible duplicate of [Document.write issues](http://stackoverflow.com/questions/8648365/document-write-issues) – Hacketo Mar 26 '15 at 08:33

1 Answers1

1

It looks like you have a syntax error on line 18:

         document.write("<h1> <font face="Calibri"> The stock ID you entered is invalid! It must only contain digits! </font> </h1>");

The double quotes for your 'face' attribute are interfering with the quotes being used for your document.write() call. To fix this, you can use single quotes for your document.write() call, or escape your double-quotes in your 'face' attributes:

         document.write('<h1> <font face="Calibri"> The stock ID you entered is invalid! It must only contain digits! </font> </h1>');

or

 document.write("<h1> <font face=\"Calibri\"> The stock ID you entered is invalid! It must only contain digits! </font> </h1>");
kennybytes
  • 41
  • 3