0

Hello, I'm trying to make a simple search with Javascript to search a form, I need to pull the index of my search term. This is what I have so far, but I'm seriously having issues. Any help is appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Form</title>
<script language="JavaScript" type=text/javascript>
function search(sub, string) {
    var where=str.indexOf(toSearch);
      alert(where);
if(where =="") ||(where == null))
    {
   alert("The string was not found");
   }    
}
//-->
</script>
</head>
<body>
<h3>CIW JavaScript Specialist</h3>
<hr />
<form name="myForm">
<p>
<strong>Look for:</strong>
<input type="text" name="what" size="20" />
</p>
<p>
<strong>in this string:</strong>
<textarea name="toSearch" rows="4" cols="30" wrap="virtual">
<body>
<h3>CIW JavaScript Specialist</h3>
<hr />
<form name="myForm">
<p>
<strong>Look for:</strong>
<input type="text" name="what" size="20" />
</p>
<p>
<strong>in this string:</strong>
<textarea name="toSearch" rows="4" cols="30" wrap="virtual">
</textarea>
</p>
<p>
<input type="button" value="Search" onclick="search(myForm.what.value, myForm.toSearch.value);" />
</p>
</form>
</body>
</html>

Edited by pritaeas: Fixed code tags

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Taywin
0

The indexOf() function for string or array returns -1 if not found, not an empty string or null...

Your major issue should be with the browser is IE (not sure if the current version still has this problem) because it does not have the build-in indexOf() function. Is that what you are asking for as well?

Edited by Taywin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.