[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6992
  • Last Modified:

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------------------------------------------------
0
astrohelp
Asked:
astrohelp
  • 6
  • 5
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now