SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Help on script

  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)

    Help on script

    I tried geeting the part in red to work, but I know its wrong. Firstly I want it to display in a <p id="rank"> fassion rather than a form. I want it to display an answer based on the totalGP value. But I am stuck.
    Code:
    <script type="text/javascript">
    function calculate() {
    	var val1 = parseFloat(document.getElementById("mycredits").value);
    	var val2 = parseFloat(document.getElementById("mybytes").value);
    	var val3 = parseFloat(document.getElementById("mymarks").value);
    	var val4 = parseFloat(document.getElementById("myexp").value);
    	var val5 = parseFloat(document.getElementById("myarena").value);
    	var val6 = parseFloat(document.getElementById("myclearing").value);
    	var val7 = parseFloat(document.getElementById("myother").value);
    	
    	var nval1 = val1 / 500000;
    	var nval2 = val2 * .4;
    	var nval3 = val3 * .002;
    	var nval4 = val4 / 1000000;
    	var nval5 = val5 / 100;
    	var nval6 = val6 / 50;
    	var nval7 = val7 / 10000;
    		
    	var totalGP = nval1 + nval2 + nval3 + nval4 + nval5 + nval6 + nval7;
    	
    	var paraAnswer = document.getElementById("answer");
    	paraAnswer.innerHTML = totalGP;
    	
    	var difference = totalGP;
    	
    	if (difference < 1) {
    	form.comment.value="Rank 1";
    	}
    	if (difference > 2 && difference < 10) {
    	form.comment.value="Rank 2";
    	}
    	if (difference > 10 && difference < 30) {
    	form.comment.value="Rank 3";
    	}
    	if (difference > 31 && difference < 50) {
    	form.comment.value="Rank 4";
    	}
    	if (difference > 51 && difference < 80) {
    	form.comment.value="Rank 5";
    	}
    	if (difference > 80) {
    	form.comment.value="Rank 6"; 
    	}
    	
    }
    </script>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The simplest way to switch it from updating a form field to replacing the content of a paragraph would be to replace:

    form.comment.value

    with

    document.getElementById('rank').innerHTML

    The more difficult way is to use the appropriate document object model commands.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    Code JavaScript:
    function pFIdVal(id){return parseFloat(document.getElementById(id).value);}
    function calculate(){
    var v1=pFIdVal('mycredits')/500000;
    var v2=pFIdVal('mybytes')*0.4;
    var v3=pFIdVal('mymarks')*0.002;
    var v4=pFIdVal('myexp')/1000000;
    var v5=pFIdVal('myarena')/100;
    var v6=pFIdVal('myclearing')/50;
    var v7=pFIdVal('myother')/10000;
    var tG=v1+v2+v3+v4+v5+v6+v7;
    document.getElementById('answer').innerHTML=tG;
    var iR;
    if(tG<1){iR='Rank 1';}
    if(tG>2&&tG<10){iR='Rank 2';}
    if(tG>10&&tG<30){iR='Rank 3';}
    if(tG>31&&tG<50){iR='Rank 4';}
    if(tG>51&&tG<80){iR='Rank 5';}
    if(tG>80){iR='Rank 6';}
    document.getElementById('rank').innerHTML=iR;
    }

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    Mirek, that looks like it should work but it doesn't. I don't have the slightest clue why.

  5. #5
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I test everything before post and it works for me well. Whole code incl. html

    <script type="text/javascript">
    function pFIdVal(id){return parseFloat(document.getElementById(id).value);}
    function calculate(){
    var v1=pFIdVal('mycredits')/500000;
    var v2=pFIdVal('mybytes')*0.4;
    var v3=pFIdVal('mymarks')*0.002;
    var v4=pFIdVal('myexp')/1000000;
    var v5=pFIdVal('myarena')/100;
    var v6=pFIdVal('myclearing')/50;
    var v7=pFIdVal('myother')/10000;
    var tG=v1+v2+v3+v4+v5+v6+v7;
    document.getElementById('answer').innerHTML=tG;
    var iR;
    if(tG<1){iR='Rank 1';}
    if(tG>2&&tG<10){iR='Rank 2';}
    if(tG>10&&tG<30){iR='Rank 3';}
    if(tG>31&&tG<50){iR='Rank 4';}
    if(tG>51&&tG<80){iR='Rank 5';}
    if(tG>80){iR='Rank 6';}
    document.getElementById('rank').innerHTML=iR;
    }
    </script>
    <input type="text" id="mycredits" value="10">
    <input type="text" id="mybytes" value="10">
    <input type="text" id="mymarks" value="10">
    <input type="text" id="myexp" value="10">
    <input type="text" id="myarena" value="10">
    <input type="text" id="myclearing" value="10000">
    <input type="text" id="myother" value="10">
    <p id="answer">answer</p>
    <p id="rank">rank</p>
    <input type="button" onclick="calculate()" value="calculate">

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    hmm, dont know what I was doing. thank you very much though.


Bookmarks

Posting Permissions

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