Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

event bubble

Posted on 2006-07-19
1
304 Views
Last Modified: 2011-09-20
I have one form in a aspx page. I have to have one form due to the design of the runat server elements.

I need the main form to submit when the enter key is press via mainonkeydown(event) or when the submit button is pressed.

However if the focus is in the "searchstring" input then subonkeydown(event) needs to be triggered when the enter key is press and the event that bubbles for the main form onsubmit needs to be cancelled.

I can get the side form to work only when I return false in the form onsubmit. Help.

Sample code below:


<script>
      function mainonkeydown(event)
      {
        if(event.keyCode == 13)
        {
                  document.Form1.submit();
        }
      }
                  
      function subonkeydown(event)
      {
          if(event.keyCode == 13)
        {
        document.location.href='page.aspx?searchstring=' + document.Form1.searchstring.value;
        }
      }
</script>


<form id="Form1" onkeydown="mainonkeydown(event);" method="post">

<input type=text name="searchstring" onkeydown="subonkeydown(event)">
<input type=button value="Go" onclick="document.location.href='page.aspx?searchstring=' + document.Form1.searchstring.value;">

<input type=text name="mainsearch1">
<input type=text name="mainsearch2">
<input type=text name="mainsearch3">
<input type=text name="mainsearch4">
<input type=button value="Submit">

</form>
0
Comment
Question by:hamishd
1 Comment
 
LVL 30

Accepted Solution

by:
third earned 500 total points
ID: 17136840
do you mean,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Author: Third Santor</title>
<script>
  var willReload = false;

  function validate(obj){
    if(willReload){
        window.location = 'page.aspx?searchstring=' + obj.searchstring.value;
//        window.location = 'http://www.google.com';
        return false;
      }
      else{
        //make your usual validation and return depending on the result
        alert('This will submit the form');
        return true;
      }
  }
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<form id="Form1" method="post" onsubmit="return validate(this);">

<input type=text name="searchstring" onfocus="willReload=true;" onblur="willReload=false">

<input type=text name="mainsearch1">
<input type=text name="mainsearch2">
<input type=text name="mainsearch3">
<input type=text name="mainsearch4">
<input type="submit" value="Submit">

</form>
</body>
</html>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
innerHTML 7 34
Diminish Pop-up  in 3 seconds 7 47
Underline in nav for the page that you are currently on. 1 22
what is the best Integrated development environment 2 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question