Javascript invalid character issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasmel
    New Member
    • Jul 2008
    • 4

    Javascript invalid character issue

    Hello,

    Sorta new with JavaScript... so don't kill me with jargon.

    I am trying to make a script that will only allow text to be inputted into a textarea that is from a drop down menu. My problem is the textarea/textbox id is pre-set and unchangeable by the shopping cart system I use. (*Changing it will make it un-upgradeable in the future)

    Here is what I have so far:

    Code:
    <html>
    <head>
    <title>Dropdown to Textbox</title>
    <script type="text/javascript">
    function setName(child)
    {
    
    	 document.all.attrib-1-0.value = child.options[child.selectedIndex].text;
    
    }
    </script>
    </head>
    <body>
    
    <select name="name" onchange="setName(this)">
    <option value="001">AJ</option>
    <option value="002">Andrew</option>
    <option value="003">Andy</option>
    </select>
    
    <input type="text" name="id[txt_1]" size="32" maxlength="32" value="" id="attrib-1-0" />
    
    </body>
    </html>
    If you replace "attrib-1-0.value" with "foo" in both the script and the text field you will see a working version of the code. It's the invalid characters that mess it up. Now, I have tried to comment the invalid characters out with "/" but it didn't work.

    Any suggestions?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Firstly, change document.all to document.getEle mentById. For the ID, use a string:
    [code=javascript]document.getEle mentById("attri b-1-0")...[/code]

    Comment

    • jasmel
      New Member
      • Jul 2008
      • 4

      #3
      ok...

      That suggestion helped me with another issue I was having: (using the getElementById)

      Code:
      <script type="text/javascript">
      function notEmpty(){
      	var myTextField = document.getElementById('attrib-1-0');
      	if(myTextField.value != "")
      		alert("You entered: " + myTextField.value)
      	else
      		alert("Please select child's name from dropdown menu")		
      }
      </script>
      however, I still can't get the selected option to go into the textbox using the getElementById

      Code:
      <html>
      <head>
      <title>Dropdown Option To Textbox</title>
      
      <script type="text/javascript">
      function notEmpty(){
      	var myTextField = document.getElementById('attrib-1-0');
      	if(myTextField.value != "")
      		alert("You entered: " + myTextField.value)
      	else
      		alert("Please select child's name from dropdown menu")		
      }
      </script>
      
      <script type="text/javascript">
      function setName(child)
      {
      
      	 document.getElementById.("attrib-1-0") = child.options[child.selectedIndex].text;
      
      }
      </script>
      </head>
      <body>
      
      <select name="name" onchange="setName(this)">
      <option value="001">AJ</option>
      <option value="002">Andrew</option>
      <option value="003">Andy</option>
      </select>
      
      <input type="text" name="id[txt_1]" size="32" maxlength="32" value="" id="attrib-1-0" />
      
      <input type='button' onclick='notEmpty()' value='Submit' />
      
      </body>
      </html>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You're not using it correctly. Look how you used it in the notEmpty() function.
        Code:
        document.getElementById("attrib-1-0").value = child.options[child.selectedIndex].text;

        Comment

        • jasmel
          New Member
          • Jul 2008
          • 4

          #5
          I also just remembered to put a readonly on the text box as well. Hopefully this will stop folks from ordering the wrong child's names.

          Originally I had a drop down menu of child's names then the customer had to input the data into the correct text box. It became a nightmare..

          Thanks again!

          Comment

          • jasmel
            New Member
            • Jul 2008
            • 4

            #6
            I used your readOnly script found here:

            http://www.w3schools.c om/htmldom/prop_text_reado nly.asp

            You're the best!

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              If you needed the value instead of the names, you could replace ".text" with ".value" or even document.getEle mentById("attri b-1-0").value.

              Just a point to bear in mind: client-side error checking is not enough, it's only a convenience. Someone could modify or turn off JavaScript. The real checking should take place on the server-side. You may already have done, but thought I'd just make sure.

              Oh, and I'm glad to see that you're happy with the service! ;)

              Comment

              Working...