Results 1 to 3 of 3

Thread: Modify hidden fields after onsubmit

  1. #1
    Join Date
    May 2014

    Unhappy Modify hidden fields after onsubmit


    I am struggeling with onsubmit and changing the values of hidden fields with JS before the form is sent.
    some Code:

    Sample HTML:
    <form method="_GET" action="gps.php" id="form1" onsubmit="return submitForm1();">
    <input type="hidden" name="foo" id="foo" value="0">
    <div onclick="$('#form1').submit();">Submit the form</div>


    function submitForm1() {
    $('#foo').val('foo new value');
    return true;

    When I submit the form a message box appears and tells me "foo new values". On my server the corresponding GET-Value which is sent is still "0"

    This ist just an example - in real world I am doing GEOCODING of an address or using geo.location for reading gps coords.

    Any help is appreciated.

  2. #2
    Join Date
    Mar 2007
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Another excellent example of everything wrong with jQuery... and with not building a form PROPERLY. There isn't even such a thing as _Get as a method. (what is it with these fictional made up METHOD's everyone's using lately?)

    First let's fix the form:
    <form method="get" action="gps.php" id="form1">
    		<input type="hidden" name="foo" id="foo" value="0" />
    		<input type="submit" value="Submit the form" />
    Then intercept the submit properly:

    (function(d) {
    		foo = d.getElementById('foo'),
    		form = d.getElementById('form1');
    	function eventAdd(e, event, handler) {
    		if (e.addEventListener) e.addEventListener(event, handler, false);
    			else this.attachEvent('on' + event, handler);
    	if (form) eventAdd(form, 'submit', function(e) {
    		foo.value = 'foo new value';
    Anonymous function so there's little risk of conflicting with other code, uses the proper getElementById methods, attaches the event from the scripting instead of the markup attaching to the script, gets rid of that bizarre value wrapping jQuery has for no good reason -- and as such, look ma, No jQuery.

    Cleaner, simpler, faster, more effective, more likely to leverage caching -- without the library nonsense.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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