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 5 of 5
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Javascript to Write Within the Existing Webpage

    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

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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>
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    or a javascript to give the form items a value

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brandonH View Post
    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

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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