Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    •  
      CommentAuthorTetsuo
    • CommentTimeJul 31st 2007 edited
     permalink
    Hi all

    I'm close to working this out, but just can't seem to get my head around it.

    I need to have one of my form buttons "click" itself on a HTML page load, in order to refresh a calculator function that is present on every page. At the moment on the page load, the input field (total) is left blank and requires the user to click the button to display the total again.

    I've seen examples of this, but they always mention a type SUBMIT button on a page load, whereas I just need to have a type BUTTON load/click on a page load.


    function myfunc () {
    var frm = document.getElementById("foo");
    frm.submit();
    }
    window.onload = myfunc;

    This one uses JS, but submits the page, rather than clicking a button present in the form.

    I've also tried numerous "body onloads", but could not get these to do anything:
    < body onload="document.forms.quoteyform.calculate(this)" >


    Where am I going wrong? :\

    Here is the form & button I need to self-click itself on a page load:

    < form id="quoteyform" action="whatever.foo" onsubmit="return false" >
    < input type="text" class="total" name="result" id="result" maxlength="9" />
    ...............
    < input type="button" value="" onclick="calculate(this)" class="rememberMe" id="test" name="test" /> This updates the "total" area above
    < /form>
    • CommentAuthorMatt
    • CommentTimeJul 31st 2007
     permalink
    document.quoteyform.submit(); and give the form name the value of quoteyform as well. You don't need to use getelementbyid function.
    •  
      CommentAuthorTetsuo
    • CommentTimeJul 31st 2007
     permalink
    < body onload="document.quoteyform.submit();"> ?

    Tried that, it doesn't do anything.

    Like I said, I need to click the button, not send or submit the form. (There isn't even a submit button on the form anyway).
    •  
      CommentAuthorTetsuo
    • CommentTimeAug 1st 2007
     permalink
    OK, I've found this example which in theory is exactly what I'm after, it clicks a button on the page load, but when I attempt to do this on my pages, I cannot get it to work on my button in my page.

    Upon further investigation, I found this, which mentions something:

    "The getElementById() method doesn't work in XML". In an XML document, you must search through attributes that have the type id, and this type must be declared in the XML document's Document Type Definition (DTD)."


    I think this is why the function is not finding my button on my page. Furthermore, "document.getElementById('maindiv').getElementsByTagName("p");" gets me to think that I need to specify the document structure in the function in order to get the JS to click my button.

    Any ideas anyone?
    • CommentAuthorMatt
    • CommentTimeAug 1st 2007
     permalink
    This would be loads easier if you posted a link to a live page of what you have so far.
    •  
      CommentAuthorjernigani
    • CommentTimeAug 1st 2007
     permalink
    try this
    You need to tell a function to go when the page loads on a 'button click'; that button must have the function inside of it.
    Since you don't want to submit the form, just have the onload activate the button. Which in turn activates your script. I'm assuming your script knows what to do after that.

    Something like that below should work, but it would be nice if we could see your script.
    <head>
    <script>
    function activateCalc()
    {
    document.getElementById('buttonCalc').click();
    }
    /*I'm assuming your calculate function already exists*/
    </script>
    </head>
    <body onload="activateCalc()">
    <form id="quoteyform" onsubmit="return false" ><input type="hidden" name="phpMyAdmin" value="4594f30712f4fabaff6997416810f3f2" />
    ....whatever else goes here....
    <input type="button" onclick="calculate(this)" value="test button" id="buttonCalc" name="buttonCalc" />
    </form>
    </body>
    •  
      CommentAuthorTetsuo
    • CommentTimeAug 2nd 2007 edited
     permalink
    Thanks guys. Apologies for not posting a URL, here it is.

    The page is long, but the only parts of the code you need to look at is the top and bottom really. The button that needs to be clicked is the "Refresh Total" button which should update the TOTAL field near the top.

    jernigani, yes, my button has some functionality on it that works fine when clicked normally. I had tried that method with no success, and implemented your code too just to make sure mine wasn't wrong, but it still isn't clicking the button. I really don't understand it. I have a feeling that it is something to do with referencing the button incorrectly, or the position of the script(s) in the code. I will carry on trying to find a solution...

    Many thanks all.
    •  
      CommentAuthorjernigani
    • CommentTimeAug 2nd 2007
     permalink
    why don't you just run the calculate function onload?
    You don't need to press the button or submit the form, you just need to run the function; Since the function changes the total value anyways.

    instead of 'this' reference the actual form, which 'this' is referring to and just run the function.

    make sure that you give the form tag the attribute ( name="quoteyform" ) otherwise some browsers won't know what to do with getElementById

    onload="calculate(document.getElementById('quoteyform'))"

    I hope that helps.
    •  
      CommentAuthorTetsuo
    • CommentTimeAug 2nd 2007
     permalink
    Yes, I think that is the best way to go jernigani.

    I've included this onload function on the body tag and pudated the test page, but it still wont work - are you sure that this calculate function is doing anything? Wouldn't it have to reference a button to click instead of the form itself?

    Thanks for your help
    •  
      CommentAuthorjernigani
    • CommentTimeAug 2nd 2007
     permalink
    hmm, yeah I don't think you can use the calculate function from onload
    are you sure this didn't work?

    <head>
    <script>
    function activateCalc()
    {
    document.getElementById('buttonCalc').click();
    }
    </script>
    </head>
    <body onload="activateCalc()">
    <form id="quoteyform" ><input type="hidden" name="phpMyAdmin" value="4594f30712f4fabaff6997416810f3f2" />
    ....whatever else goes here....
    <input type="button" onclick="calculate(this)" value="test button" id="buttonCalc" name="buttonCalc" />
    </form>
    </body>

    Make sure on the button that the calculate function is in an onclick attribute not an onchange or anything else. it has to be an onclick.

    You might also try, instead of an onload in the body tag, running the activate function at the bottom of the page. Make sure you declared the function at the top of the page though, or that won't work.


    <script>
    activateCalc();
    </script>
    </body>
    •  
      CommentAuthorTetsuo
    • CommentTimeAug 2nd 2007
     permalink
    Yeh, I've tried your example but to no avail unfortunately. I'd only used that onchange in the code because I was trying different things. Also tried moving scripts around the page too, but it still don't work.

    Infact, using an "onfocus" I could see (in Opera, anyway) that the function was correctly "targeting" the button, but the click never works :(
    •  
      CommentAuthorjernigani
    • CommentTimeAug 2nd 2007 edited
     permalink
    sorry, I don't know what else to tell you. I don't know enough about your functions to help you figure it out. In your calculate function there is a getTotal function that you might try working with.
  1.  permalink
    Well actually you've got an issue with your current usage of click(). You've loaded the function to the event before the function was defined. Also if you just did: calculate(document.getElementById("buttoncalc")) You should be fine. This whole click()ing a button to fire a function thing is pretty ridiculous.
Add your comments
    Username Password
  • Format comments as (Help)