Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Senior Member Tomaszewski's Avatar
    Join Date
    Oct 2006
    Location
    Exton, PA
    Posts
    194
    Member #
    14132
    hi, i have this little issue, i'm trying to make the SIMPLEST google search for a cool little project and when i try to load a link via JS on form submit, it doesn't do anything. Changing the submit button to a type="button" and adding the event to that button onclick="function()" will work, but add the onsubmit="function()" to the <form> tag won't work. Can someone tell me why? Or how I can get it to work?



    Why wont' this work?

    HTML Code:
    //JS file
    			function showSearchWord()
    			{
    				var googleSearch = "http://www.google.com/search?q=";
    				var formsValue = document.getElementById("googleForm").value;
    				window.location = googleSearch + formsValue;
    			}
    
    //html file
    			<form name="myForm" onsubmit="showSearchWord()">
    				<ul>
    					<li>
    					<input type="text" id="googleForm" name="inputForm" />
    					<input type="submit" value="Search" />
    					</li>
    				</ul> 	
    			</form>
    ... and why does this work?

    HTML Code:
    //JS file
    			function showSearchWord()
    			{
    				var googleSearch = "http://www.google.com/search?q=";
    				var formsValue = document.getElementById("googleForm").value;
    				window.location = googleSearch + formsValue;
    			}
    
    //html file
    			<form name="myForm">
    				<ul>
    					<li>
    					<input type="text" id="googleForm" name="inputForm" />
    					<input type="button" value="Search" onclick="showSearchWord()"/>
    					</li>
    				</ul> 	
    			</form>

  2.  

  3. #2
    Senior Member iphong's Avatar
    Join Date
    Mar 2003
    Location
    Arlington, VA
    Posts
    751
    Member #
    916
    This function needs to return false other why it will take you to the action page first before execute your code . In your case your action page is blank so it's automatically set to the current page.

    and by the way, onsubmit does not work with Firefox though.

    This should work:

    Code:
    // Javascript
    function showSearchWord()
    {
    	var googleSearch = "http://www.google.com/search?q=";
    	var formsValue = document.getElementById("googleForm").value;
    	window.location = googleSearch + formsValue;
    	
    	return false;
    }
    
    //HTML
    <form name="myForm" onsubmit="return showSearchWord();">
        <ul>
            <li>
            <input type="text" id="googleForm" name="inputForm" />
            <input type="submit" value="Search" />
            </li>
        </ul> 	
    </form>
    Vu Tuan Phong
    iPhongDesign

    http://iphong.com
    http://adstudio1.com


Remove Ads

Posting Permissions

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

Search tags for this page

form action window location

,

ul li onsubmit

Click on a term to search for related topics.
All times are GMT -6. The time now is 05:35 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com