SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a function from a form written in document.write

    I have a form on the page (name="calculator") that when a button is clicked calls a function that rewrites the page with another form. I want to call a 2nd function from the 2nd form's button that refreshes the page.

    The 2nd function I wish to call is:
    function reFresh() {
    window.location.reload(true);
    }

    The 2nd form that is called is:
    document.write('<form id="calcResult" name="calcResult" />')
    document.write('<p class="submit"><input type="button" value="Get Another Quote" onclick="reFresh()" /></p>')
    document.write('</fieldset>')
    document.write('</form>')

    But it doesn't work. Why?

    Note: If I call the function reFresh() from the first form on the page then it works - but when called from the form that is written dynamically it doesn't work.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using document.write on a document that has finished loading will erase everything that was there (including functions and variables).

    You should add the new form using the DOM, here is an example - for simplicity I added a DIV around the form.
    Code:
    var oDiv = document.createElement("div");
    oDiv.innerHTML = '<form id="calcResult" name="calcResult"><p class="submit"><input type="button" value="Get Another Quote" onclick="reFresh()" /></p></form>';
    document.appendChild(oDiv);


Bookmarks

Posting Permissions

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