Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question how to write text/integer from a function

    Hi, I was hoping you could help me, I was wanting to write text to the screen but without re-writing the whole screen (e.g. document.write();. I wish to take text and put it in a function, modify it, then display it.
    So far I have:

    Code:
    <script type = "text/javascript">
    		function calculate(text){
    				var x = "10.0";
    				var gpa = text.replace(/0.0/g,x);
    				document.getElementById("text").innerText=gpa;				
    			}
    		</script>
    		<p id = text >Your Grade Point Average (GPA) is: 0.0</p>
    		<button type="button" onclick="calculate(text)">Calculate</button>
    Your help would be really appreciated
    Last edited by sophie_pocket; 12-08-2011 at 03:48 PM. Reason: missing words

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Code:
    <script type = "text/javascript">
    		function calculate(text){
    				var text = document.getElementById(text);
    				var x = "10.0";
    				var gpa = text.innerHTML.replace(/0.0/g,x);
    				text.innerHTML=gpa;				
    			}
    		</script>
    		<p id = "text" >Your Grade Point Average (GPA) is: 0.0</p>
    		<button type="button" onclick="calculate('text')">Calculate</button>
    Code:
    <script type = "text/javascript">
    		function calculate(elementId){
    				var el = document.getElementById(elementId);
    				var x = "10.0";
    				var gpa = el.innerHTML.replace(/0.0/g,x);
    				el.innerHTML=gpa;				
    			}
    		</script>
    		<p id = "textP" >Your Grade Point Average (GPA) is: 0.0</p>
    		<button type="button" onclick="calculate('textP')">Calculate</button>
    Last edited by DaveyErwin; 12-08-2011 at 03:09 PM.

  • Users who have thanked DaveyErwin for this post:

    sophie_pocket (12-08-2011)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Thank you so much! it works perfectly


  •  

    Tags for this Thread

    Posting Permissions

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