Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2011, 01:43 PM   PM User | #1
sophie_pocket
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
sophie_pocket is an unknown quantity at this point
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
sophie_pocket is offline   Reply With Quote
Old 12-08-2011, 02:11 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 814
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
sophie_pocket (12-08-2011)
Old 12-08-2011, 02:25 PM   PM User | #3
sophie_pocket
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
sophie_pocket is an unknown quantity at this point
Smile

Thank you so much! it works perfectly
sophie_pocket is offline   Reply With Quote
Reply

Bookmarks

Tags
functions, javascript, variables, write

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:34 AM.


Advertisement
Log in to turn off these ads.