www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] body onload() help

  1. #1
    Join Date
    Sep 2006
    Posts
    7

    resolved [RESOLVED] body onload() help

    I am trying to teach myself a little bit of Javascript. Something I have always wanted to do.

    From an example I found on the web, you select the qty from the drop down menu, which is then multiplyed to the value 0.10
    The output of this calculation is then displayed in the div

    I added the body onload() so that when the page is accessed the function is initiated. The qty 1 is already selected and automatically calculated.
    It kind of works but the result I get in the div is 0.01

    Wondering if someone could help me out here. Code is below

    HTML Code:
    <body onload="dropdownTip(0.10);">
    
    <script type="text/javascript">
    function dropdownTip(value){
        console.log(value);
            document.getElementById("result").innerHTML = (value * 0.10).toFixed(2);
        }
    	</script>
    
    <select onChange="dropdownTip(this.value)" name="quantity" style="margin-right:10px; margin-top:2px;">
        <option selected="selected" value="1">1</option>    
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
    </select>
    
    <table width="200" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="8">&pound;</td>
        <td width="178"><div id="result"></div></td>
      </tr>
    </table>

  2. #2
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    not sure what school you went to but my teachers (bless their souls) taught 0.1 times 0.1 always came to 0.01

  3. #3
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Quote Originally Posted by ade234uk View Post
    automatically calculated.
    OK I was being facetious with my previous reply.


    But automatically calculated is definitely not the case....

    Add the JavaScript function below yours

    Code:
    function autoCalcBit(){
        var op = document.getElementsByTagName('select')[0].value;
       dropdownTip(op);
    }
    and the change your body onload to

    Code:
    <body onload="autoCalcBit();">
    you will have to get that teacher of yours to tell you about the error console...

  4. #4
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by grumpyOleMan View Post
    not sure what school you went to but my teachers (bless their souls) taught 0.1 times 0.1 always came to 0.01
    I can do my times tables lol. Got the code wrong that's all. I have seen the error and resolved.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles