Web Analytics Made Easy -
StatCounter "Escape a Space" ??? what? - CodingForum

Announcement

Collapse
No announcement yet.

"Escape a Space" ??? what?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • "Escape a Space" ??? what?

    I have recieved information on how to do a job but I was instructed to "escape a space" using javascript. What does that mean?

    I have a radio button that MUST have a space in the name and I'm trying to reference the value of that button. For example

    <input type="radio" name="MyProduct Option" value="$23">

    function showPrice(name){
    var price;
    document.myform[name].value = price
    alert(price)
    }

  • #2
    when you want to include characters, that have a meaning with regards to the programming language, in a string, then you have to escape them, so that they'll be interpreted as a part of the string, and the compiler or interpreter, won't try to perform their function.

    examples:

    var string0 = 'This is a string with \'escaped\' characters in it';

    var string1 = 'This is a string has an \'escaped\' backslash (\\) in it';

    alert(string0);
    alert(string1);

    there are other ways to escape characters in javascript, but that's the most common one.

    i'm pretty sure that in this particular case, you don't need to escape the space, unless there's more to this than just the code you posted.

    you could accomplish your goal like this:

    function showPrice(name){
    var price;
    document.myform.elements[name].value = price;
    alert(price);
    }
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

    Comment


    • #3
      Thanks joh6nn,

      So if I wanted to create a radio button that would 'uncheck' or 'unselect' another radio button why doesn't this work?:

      function changeStatus(name,status) {

      document.flightpurchase.elements[name].checked = status
      }

      <form name="flightpurchase">
      Click Here to uncheck the second Radio Button
      <input type="radio" name="First Radio" onClick="changeStatus('Second Radio',0)"><br><br>Second Button
      <input type="radio" name="Second Radio" checked>

      Comment


      • #4
        jwoods, i apologize. i wasn't paying enoug attention the first time, and i missed the part where you said you must have a space in the name of your radio button. as far as i know, that's impossible. you can't have spaces in the names of HTML elements, not even escaped.
        bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

        i am a loser geek, crazy with an evil streak,
        yes i do believe there is a violent thing inside of me.

        Comment


        • #5
          You could reference the second radio button by its position in the forms array. Here is an example that will uncheck the second radio when you click on the first.
          Code:
          <html>
          <head>
          <title>Forms Test</title>
          <SCRIPT LANGUAGE="JavaScript">
             function unchk(whichone,status)
               {
                document.myform.elements[whichone].checked=status
               }
          </SCRIPT>
          </head>
          <body>
            <FORM NAME="myform">
             First Radio  <INPUT TYPE="radio" NAME="First Radio" onClick="unchk(1,0)">
             Second Radio <INPUT TYPE="radio" NAME="Second Radio">
            </FORM>
          </body>
          </html>
          Each element in a form can be referenced this way when the name is not possible. This is especially useful when you are generating a form and don't know how may elements you will have.

          Comment


          • #6
            i'll be real surprised if that works. to the best of my knowledge, you simply can't have spaces in the names of HTML elements.
            bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

            i am a loser geek, crazy with an evil streak,
            yes i do believe there is a violent thing inside of me.

            Comment


            • #7
              function showPrice(name){
              var price = document.myform[name].value;
              alert(price);
              }

              Nothing like alert()ing an unassigned variable.

              Perfectly OK using spaces in certain HTML properties; just make sure, if you reference them in JS, to use hash notation:

              document.myform['MyProduct Option'].value

              ...and not dot notation:

              document.myform.MyProduct Option.value

              ...as you'll have 'broken' a token and will be notified as such.

              Comment

              Working...
              X