Solved

event bubble

Posted on 2006-07-19
1
301 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now