Solved

event bubble

Posted on 2006-07-19
1
303 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

770 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