We help IT Professionals succeed at work.

Javascript indexOf problem - indexOf is not a function

astrohelp
astrohelp asked
on
Medium Priority
7,492 Views
Last Modified: 2008-01-09
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------------------------------------------------
Comment
Watch Question

Hi astrohelp,

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

Joe P

Author

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. :)
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

Commented:
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

Commented:
Or maybe it's

var str=textObject.value;

Pardon my haste.

Peace and joy to you.
mvan

Author

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)

Author

Commented:
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
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

Author

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!

Author

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!
No problem. =)

Joe P
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.