string().replace() usage...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    #1

    string().replace() usage...


    Folks,

    Can someone help my syntax?

    var z=myForm.myInpu tTag.value.stri ng().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
  • Spats30

    #2
    Re: string().replac e() usage...

    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.

    Comment

    • RobB

      #3
      Re: string().replac e() usage...

      Randell D. wrote:[color=blue]
      > Folks,
      >
      > Can someone help my syntax?
      >
      > var z=myForm.myInpu tTag.value.stri ng().replace(" ","-");
      >
      > I would expect the above to allow z to take the value of an input tag[/color]
      [color=blue]
      > (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[/color]

      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.getEle mentById('myFor m') 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.val ue.replace(/ /g, '-');

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

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

      Comment

      • Randell D.

        #4
        Re: string().replac e() usage...

        RobB wrote:[color=blue]
        > Randell D. wrote:
        >[color=green]
        >>Folks,
        >>
        >>Can someone help my syntax?
        >>
        >> var z=myForm.myInpu tTag.value.stri ng().replace(" ","-");
        >>
        >>I would expect the above to allow z to take the value of an input tag[/color]
        >
        >[color=green]
        >>(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[/color]
        >
        >
        > 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.getEle mentById('myFor m') 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.val ue.replace(/ /g, '-');
        >
        > If you wanted *one* dash, even for multiple spaces:
        >
        > var z = document.myForm .myInputTag.val ue.replace(/ +/g, '-');
        >[/color]


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

        Cheers
        Randell D.

        Comment

        • Randell D.

          #5
          Re: string().replac e() usage...

          Spats30 wrote:
          [color=blue]
          > 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.
          >[/color]

          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.

          Comment

          Working...