View Full Version : Focus

Jon W
06-17-2009, 01:23 AM
So I'm a noobie at JavaScript and I'm messing around and learning things here and there well I ran into a small problem. I made a simple form like a search form and created a function that check and see if the input field that is suppose to be where for example the user types in her/her search term. And the message pops up and displays everything like it should but after it shows the message the form continues on and goes onto the next page. And I don't want that. I just want it to end everything until the user types in a search term. So here is my script.

<script type="text/javascript">
function myFun() {

var find = document.getElementById('find');

if(document.getElementById('find').value=='') {
alert("You must type in a search term.");
return false;


Thanks for the help.

Old Pedant
06-17-2009, 02:03 AM
Show where and how you *call* that function.

If you are doing something like:

<form ... onsubmit="myFun( );">

then you need to change it to

<form ... onsubmit="return myFun( );">

Same would apply if you are using an image button or submit button and doing onclick="myFun( )"

If something other than those, show more code.

Jon W
06-17-2009, 03:28 AM
The code is called by this here:

<form action="http://localhost" method="get" onsubmit="myFun()">
<input name="input" id="find" type="text" />
<input name="submit" value="Search" type="submit" />

Sorry it took me so long I took a little nap. :p lol long day. Even with onsubmit="myFun()" it still goes onto the other page.

Old Pedant
06-17-2009, 03:47 AM
*READ* what I wrote. I even put it in red. Maybe bigger will help?

<form action="http://localhost" method="get" onsubmit="return myFun()">

If you don't have that return in there, you are effectively saying "always return true."

Yes, you *also* need the return in your function.

And, incidentally, your function never returns a true value, explicitly. Usually isn't needed, but play it safe!