Link to home
Start Free TrialLog in
Avatar of Ralph
RalphFlag for United States of America

asked on

Simple Javascript problem

I'm NOT a javascripter;  this should be easy -- but it aint working!

var wireless_no = $( "input[name=wireless_no]" ).val().trim() ;
  alert(wireless_no) ;
  wireless_no = wireless_no.strip_phone_format() ;

Open in new window

And my function:
// *************************************************************************************************

String.prototype.strip_phone_format = function (phone_no)
{
  var phone=phone_no.toString() ;    // just in case.  Chrome inspector says phone_no above is undefined.
  var front=phone.replace("(","") ;
  var middle=phone.replace(") ","") ;
  var back=phone.replace("-","") ;
  
  return front+middle+back ;
} ;

Open in new window


The alert shows the 4 digits I type.
The functions insists that phone_no is undefined.

What am I missing?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You defined your function with a parameter, but when you call it, you don't pass it:

wireless_no = wireless_no.strip_phone_format() ;

Open in new window


I would define the function simply as

strip_phone_format : function (phone_no)  {
  var phone=phone_no.toString() ;
  var front=phone.replace("(","") ;
  var middle=phone.replace(") ","") ;
  var back=phone.replace("-","") ;
  return front+middle+back ;
} ;

Open in new window


and then use it like this:

var wireless_no = $( "input[name=wireless_no]" ).val().trim() ;
alert(wireless_no) ;
wireless_no = strip_phone_format(wireless_no) ;

Open in new window

Avatar of Ralph

ASKER

Dumb mistake on my part.  Thank you.
Also the code needed fixing, but that was easily done.