I use this script to search in a static page
But i want this to search when i type in the text input and not when i click the button, i searchd and i found that any of this would work:
onkeypress="this.submit();"
onkeyup="this.submit();"
onkeypress="document.forms["f1"].submit();"
onkeyup="document.forms["f1"].submit();"
but none of them works
i used the same html with the script's
<form id="f1" name="f1" action="javascript:void()" onsubmit="if(this.t1.value!=null && this.t1.value!='')parent.findString(this.t1.value);return false;">
<input type="text" id="t1" name="t1" value="Search" onfocus="if(this.value=='Search')this.value='';" size="20" onkeypress="this.submit();" />
<input type="submit" name="b1" value="Find" />
</form>
As Tom said, calling
form.submit()
does not trigger the onsubmit handlers. The onsubmit handlers are only called when the form is submitted manually. Therefore, if you are trying to submit your form manually, you have to check your errors on your own.HTML
JS
Having said all this, if you just had a
findString
that was a bit smarter, then you would just call it from both places and ignore it when emptyYou have a quote problem! The color coding here shows it!
The sumbit is on the form, not the element, hence why
this.submit
fails.You would need
Perhaps you should make use of addEventListener() to assign events. Besides, you have a typo. It would be better to use single quotes for begin/end of strings, so that inline doublequotes are no problem (vice versa).
And to submit a Form via Javascript:
submits the first form.
form.submit() does not trigger onsubmit. You should implement a function instead.
Your onkeyup script is counter-intuitive though, since selecting the text onkeyup requires blurring of the textbox focus.
Created a test using your snippets that calls
findString(this.value);
instead of submit:http://jsfiddle.net/e9Esz/
Javascript: