string().replace() usage...

R

Randell D.

Folks,

Can someone help my syntax?

var z=myForm.myInputTag.value.string().replace(" ","-");

I would expect the above to allow z to take the value of an input tag
(called myInputTag) and replace spaces with dashes...

It doesn't change the string - nor do I get errors in the Mozilla
javascript conole...

All help, via the newsgroup (for all to learn) is appreciated, thanks
randelld
 
S

Spats30

All form values are already interpretted as "string" data types.
Remove the ".string()" from your statement, and it should work. Note
that this change will only put the updated string into your variable,
not the form, as you currently have the code written.

Make the update, then do an "alert()" on the variable to make sure
everything is cool.
 
R

RobB

Randell said:
Folks,

Can someone help my syntax?

var z=myForm.myInputTag.value.string().replace(" ","-");

I would expect the above to allow z to take the value of an input tag
(called myInputTag) and replace spaces with dashes...

It doesn't change the string - nor do I get errors in the Mozilla
javascript conole...

All help, via the newsgroup (for all to learn) is appreciated, thanks
randelld

You may be thinking of the Object.toString() method; no such thing as a
..string() method. Also: is 'myForm' simply the name of the form? If so
you'll need to use either document.myForm, or
document.getElementById('myForm') with <form id="myForm"...>.
String.replace works with two string arguments, but it's far more
versatile with a regular expression and a replacement string (or
callback function):

var z = document.myForm.myInputTag.value.replace(/ /g, '-');

If you wanted *one* dash, even for multiple spaces:

var z = document.myForm.myInputTag.value.replace(/ +/g, '-');
 
R

Randell D.

RobB said:
You may be thinking of the Object.toString() method; no such thing as a
.string() method. Also: is 'myForm' simply the name of the form? If so
you'll need to use either document.myForm, or
document.getElementById('myForm') with <form id="myForm"...>.
String.replace works with two string arguments, but it's far more
versatile with a regular expression and a replacement string (or
callback function):

var z = document.myForm.myInputTag.value.replace(/ /g, '-');

If you wanted *one* dash, even for multiple spaces:

var z = document.myForm.myInputTag.value.replace(/ +/g, '-');


Thanks for that - I had used a regexp as I didn't believe it was
required... now it works...

Cheers
Randell D.
 
R

Randell D.

Spats30 said:
All form values are already interpretted as "string" data types.
Remove the ".string()" from your statement, and it should work. Note
that this change will only put the updated string into your variable,
not the form, as you currently have the code written.

Make the update, then do an "alert()" on the variable to make sure
everything is cool.

Yeah, I had tried sending the output via alert() and even tried with and
without string. I don't know why but when I replaced my arguements in
replace() with a regular expression (suggested in a follow on post from
yours) it worked...

Thanks though,
Randell D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top