Simple Javascript problem

Ralph
Ralph used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:
You function does not expect any parameters, as a result you should work with "this":
// *************************************************************************************************

String.prototype.strip_phone_format = function ()
{
  var phone=this.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

zzynxSr. Software engineer

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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial