...

View Full Version : Using Javascript to Write Within the Existing Webpage



DavidB
02-04-2007, 05:26 AM
Hi, folks.

Is there a way to dynamically write text to an existing webpage with Javascript?

I am writing a page with a solver (in Javascript) that calculates the roots of a quadratic equation.

The user inputs the polynomial coefficients via fields in a form.

I was hoping to output the results neatly below the form using Javascript’s document.write method. This way, if no imaginary components are present, an imaginary field won’t be output at all (to possibly cause confusion among high-school students who have never dealt with imaginary numbers). However, it seems that document.write cannot be used like I want from within an EventHandler-–it would overwrite the existing page. So I cannot simply tell users to click the “Find Roots” button and have the solutions output lower down on the page.

Any suggestions for accomplishing what I want?

Thanks.


David

brandonH
02-04-2007, 06:26 AM
try innerHTML


create a div or span shere you want the results to show up and do as such:


<span id=results></span>

<script type=text/javascript>

function yourfunction(){

//do your equating here
var result=//what you get from equating
document.getElementById('results').innerHTML=result;
}
</script>

FJbrian
02-04-2007, 06:50 PM
or a javascript to give the form items a value

DavidB
02-06-2007, 02:19 AM
try innerHTML


create a div or span shere you want the results to show up and do as such:


<span id=results></span>

<script type=text/javascript>

function yourfunction(){

//do your equating here
var result=//what you get from equating
document.getElementById('results').innerHTML=result;
}
</script>


Thanks for the feedback, Brandon.

However, that technique is only applicable for Internet Explorer, isn't it? I'd like my page to work on all browsers.

I'm starting think I will be stuck with a form. I may get rid of having a separate text box for imaginary components, but I will still have at least one text box for each root (if an imaginary component is present, it would just be concatenated with the real part and output in a single textbox). It doesn't give the seamless appearance I would like for my page, but it is better than what I have now.


David

felgall
02-06-2007, 03:40 AM
All modern browsers understand innerHTML but if you want to use the method recommended by the standards then read up on the Document Object Model (DOM).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum