Link to home
Create AccountLog in
Avatar of astrohelp
astrohelp

asked on

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------------------------------------------------
Avatar of BogoJoker
BogoJoker

Hi astrohelp,

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

Joe P
Avatar of astrohelp

ASKER

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. :)
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
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
Or maybe it's

var str=textObject.value;

Pardon my haste.

Peace and joy to you.
mvan
<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)
i see bogo, let me try "this.value" though I don't remember doing that in the past.
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
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
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!
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!
No problem. =)

Joe P