/    Sign up×
Community /Pin to ProfileBookmark

Returning a value in document.write()

Hi all,
I need some help about a result very strange for me?
I write a specific technical calculator. When ‘calculate’ is clicked the program has to print some results by document.write(…).
Inside it is called a function which returns a value as next:

// k_a , k_b – values taken from text input

function evalDeep(k_a,k_b){

var res=0;
var marg = 0.2;

k_a=parseFloat(k_a);
k_b=parseFloat(k_b);
res = k_a – k_b;
if (res > [B]user_input_text[/B] + marg){
alert(“too much”);
}
return res.toFixed(2)
}

The problem is in user_input_text – if I put a simple value for exam. 1.2 it works OK. If I try to interprete the real input it crashes. I try to do that as follows:

var x;

function detInput(){

x=document.getElementById(“deep”); //deep is txt input where the number is // typed
var y=parseFloat(x.value);
return y;
}

and instead of user_input_text in the first func. is detInput(). It returns the value but the execution stops! Any idea what’s wrong? Please help

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Feb 20.2008 — What happens if you do the following:
<i>
</i>function detInput() {
alert(parseFloat(document.getElementById("deep").value));
}


If it alerts NaN, thats where your problem is. Otherwise, it'd help to see the real code.
Copy linkTweet thisAlerts:
@felgallFeb 20.2008 — If you are using document.write() after the page has loaded then it will overwrite the entire page and so the script will no longer exist to finish running at that point.
Copy linkTweet thisAlerts:
@kapitanovnauthorFeb 21.2008 — If you are using document.write() after the page has loaded then it will overwrite the entire page and so the script will no longer exist to finish running at that point.[/QUOTE]

It seems this is exactly the problem : a cycle have to print five times different results (when I use a simple number for exam 1.7 it prints five times as expected; if I get the value 1.7 simulating the user input, then it prints once and stops :eek: ).

TJ111: In fact the message is just an alert that sth is not ok. By document.write() I need to display other info.

Please give a hint how to solve this
Copy linkTweet thisAlerts:
@felgallFeb 21.2008 — To update the content of the page after the page has loaded you need to use either innerHTML or the appropriate DOM commands.
Copy linkTweet thisAlerts:
@kapitanovnauthorFeb 22.2008 — Ok, thanks a lot! I'll try with updates for innerHTML instead of direct printing
×

Success!

Help @kapitanovn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...