Javascript indexOf problem - indexOf is not a function

I wrote a javascript function, mostly using the w3 schools "tryit" editor, then I converted it to a function and placed it in a page. The function worked fine in the "tryit" editor, but fails on my server with the error "indexOf is not a function". Any thoughts would be helpful! I am placing this a 500 points because I am in a hurry.

thanks

the function allows users to enter time in a text box in any of the following manners
1.00p
01.00p
1.00pm
1:00pm
01:00pm
1:00 pm
etc...

with the following output:
01:00 PM
-------------------code------------------------------------------
<html>
<head>
<script language="javascript">

function converttime(str)
{
var str=str
//check for delimiter style
if (str.indexOf(".")=="-1"){
  del = ":"
} else {
  del = "."
}

//check the length of the hours
var hlen = str.indexOf(del)

//check AM or PM
if ((str.indexOf("a")=="-1")&&(str.indexOf("A")=="-1")){
  ap="pm"
} else {
  ap="am"
}

//set variables
h = str.substr(str.indexOf(del)-hlen,hlen)
if (h<10){ h="0" + h }
m = str.substr(str.indexOf(del)+1,2)

//output time
finaltime=h + ":" + m + " " + ap
document.write("time=" + finaltime)
}//end converttime

</script>
</head>
<body>
<input type="text" name="time" onchange="converttime(this)">
</body>
</html>
---------------------end code------------------------------------------------
LVL 2
astrohelpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi astrohelp,

I think its your var str = str.  Just delete that line.

Joe P
0
astrohelpAuthor Commented:
oh... no i threw that in at the suggestion of someone else here and forgot to take it out before I posted. It was one of those "I knew it was not the answer moments" that you just have to try anyway. :)
0
BogoJokerCommented:
hehe, I agree.  I noticed a few errors I'll try and make it work.
I'ma lso a huge fan of w3schools try-it, I'm using that right now to test out your script.
For 1, convertitme(this) should pass this.value.

Joe P
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mvan01Commented:
Hi atrohelp,

I think you need to change this:
function converttime(str)
{
var str=str

to this:
function converttime(textObject)
{
var str=textObject.time.value

because you passed an object, and you need to get the value of "time" which you named it.
I haven't tested this.

Peace and joy to you.
mvan
0
mvan01Commented:
Or maybe it's

var str=textObject.value;

Pardon my haste.

Peace and joy to you.
mvan
0
astrohelpAuthor Commented:
<html>
<head>
<script language="javascript">

function converttime(str)
{

var del="";
//check for delimiter style
if (str.indexOf(".")=="-1"){
  del = ":";
} else {
  del = ".";
}

//check the length of the hours
var hlen = str.indexOf(del);

//check AM or PM
if ((str.indexOf("a")=="-1")&&(str.indexOf("A")=="-1")){
  ap="pm";
} else {
  ap="am";
}

//set variables
h = str.substr(str.indexOf(del)-hlen,hlen);
if (h<10){ h="0" + h; }
m = str.substr(str.indexOf(del)+1,2);

//output time
finaltime=h + ":" + m + " " + ap;
document.write("time=" + finaltime);
}//end converttime

</script>
</head>
<body>
<form name="form1">
<input type="text" name="time" onchange="converttime(document.form1.time.value)">
</form>
</body>
</html>

ok I changed "onchange="converttime(this)" to "onchange="converttime(document.form1.time.value)" and it works now, anyone have any thoughts why? (and I cleaned a little... sorry for the messy code)
0
astrohelpAuthor Commented:
i see bogo, let me try "this.value" though I don't remember doing that in the past.
0
BogoJokerCommented:
Here:
<html>
<head>
<script type="text/javascript">
alert("1");
function converttime(str)
{
  var ap = "";
  var del = "";
  alert("2");
  //check for delimiter style
  if (str.indexOf(".")=="-1"){
    del = ":"
  } else {
    del = "."
  }

  //check the length of the hours
  var hlen = str.indexOf(del);

  //check AM or PM
  if ( str.indexOf("a") > 0 || str.indexOf("A") > 0)
    ap = "am";
  else
    ap = "pm";

  //set variables
  var h = str.substr(str.indexOf(del)-hlen,hlen)
  if (h<10){ h="0" + h }
  var m = str.substr(str.indexOf(del)+1,2)

  //output time
  var finaltime=h + ":" + m + " " + ap
  document.write("time=" + finaltime)
}//end converttime
</script>
</head>
<body>
<input type="text" name="time" onchange="converttime(this.value);">
</body>
</html>

That works but I am gonna fix it up a LOT.

Joe P
0
BogoJokerCommented:
How about this, much cleaner don't you think!!!

<html>
<head>
<script type="text/javascript">
function converttime(str)
{
  //check for delimiter style
  var del = ".";
  if (str.indexOf(".")=="-1")
    del = ":";

  //check AM or PM
  var ap = "pm";
  if ( str.indexOf("a") > 0 || str.indexOf("A") > 0 )
    ap = "am";

  //get the length of the hours
  var hlen = str.indexOf(del);

  //set variables
  var h = str.substr(str.indexOf(del)-hlen,hlen);
  if (h<10)
    h = "0" + h;
  var m = str.substr(str.indexOf(del)+1,2);

  //output time
  var finaltime = h + ":" + m + " " + ap;
  document.write("The time is " + finaltime);

}//end converttime
</script>
</head>
<body>
<input type="text" name="time" onchange="converttime(this.value);">
</body>
</html>

Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BogoJokerCommented:
Your biggest problem was you were creating varaibles on the fly.  Without the keyword 'var'
You could eliminate your if else statements by setting the value originally to what the else was, then changing it if the 'if' was true.
Semicolons, you should really get used to using them.  They make code cleaner, much easier to read!
Your logic though was flawless, well done.

Next step: Allow input from 00:00 to 23:59!

=)
Joe P
0
astrohelpAuthor Commented:
OK bogo, you are 100% correct, no I see in some of my old code that I do in fact pass this.value, and it now works fine! I have to hammer out a few other small bugs and this will be done! thanks!
0
astrohelpAuthor Commented:
much cleaner, thanks bogo, I know the code was jumbly, it was just something I was slapping together as things came up so I was planning on cleaning once things worked at all! thanks again for all of your help!
0
BogoJokerCommented:
No problem. =)

Joe P
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.