473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Javascript invalid character issue

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Dropdown to Textbox</title>
  4. <script type="text/javascript">
  5. function setName(child)
  6. {
  7.  
  8.      document.all.attrib-1-0.value = child.options[child.selectedIndex].text;
  9.  
  10. }
  11. </script>
  12. </head>
  13. <body>
  14.  
  15. <select name="name" onchange="setName(this)">
  16. <option value="001">AJ</option>
  17. <option value="002">Andrew</option>
  18. <option value="003">Andy</option>
  19. </select>
  20.  
  21. <input type="text" name="id[txt_1]" size="32" maxlength="32" value="" id="attrib-1-0" />
  22.  
  23. </body>
  24. </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?
Jul 7 '08 #1
6 2842
acoder
16,027 Expert Mod 8TB
Firstly, change document.all to document.getElementById. For the ID, use a string:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("attrib-1-0")...
Jul 7 '08 #2
jasmel
4
ok...

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

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function notEmpty(){
  3.     var myTextField = document.getElementById('attrib-1-0');
  4.     if(myTextField.value != "")
  5.         alert("You entered: " + myTextField.value)
  6.     else
  7.         alert("Please select child's name from dropdown menu")        
  8. }
  9. </script>
however, I still can't get the selected option to go into the textbox using the getElementById

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Dropdown Option To Textbox</title>
  4.  
  5. <script type="text/javascript">
  6. function notEmpty(){
  7.     var myTextField = document.getElementById('attrib-1-0');
  8.     if(myTextField.value != "")
  9.         alert("You entered: " + myTextField.value)
  10.     else
  11.         alert("Please select child's name from dropdown menu")        
  12. }
  13. </script>
  14.  
  15. <script type="text/javascript">
  16. function setName(child)
  17. {
  18.  
  19.      document.getElementById.("attrib-1-0") = child.options[child.selectedIndex].text;
  20.  
  21. }
  22. </script>
  23. </head>
  24. <body>
  25.  
  26. <select name="name" onchange="setName(this)">
  27. <option value="001">AJ</option>
  28. <option value="002">Andrew</option>
  29. <option value="003">Andy</option>
  30. </select>
  31.  
  32. <input type="text" name="id[txt_1]" size="32" maxlength="32" value="" id="attrib-1-0" />
  33.  
  34. <input type='button' onclick='notEmpty()' value='Submit' />
  35.  
  36. </body>
  37. </html>
Jul 7 '08 #3
acoder
16,027 Expert Mod 8TB
You're not using it correctly. Look how you used it in the notEmpty() function.
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("attrib-1-0").value = child.options[child.selectedIndex].text;
Jul 7 '08 #4
jasmel
4
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!
Jul 7 '08 #5
jasmel
4
I used your readOnly script found here:

http://www.w3schools.com/htmldom/prop_text_readonly.asp

You're the best!
Jul 8 '08 #6
acoder
16,027 Expert Mod 8TB
If you needed the value instead of the names, you could replace ".text" with ".value" or even document.getElementById("attrib-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! ;)
Jul 8 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: delerious | last post by:
I see some code examples like this: <DIV onmouseover="this.style.background='blue'"> and other code examples like this: <DIV onmouseover="javascript:this.style.background='blue'"> Which...
8
by: Sue | last post by:
In this code why is it that when I press the SUBMIT button the focus only goes back to the Numeric field. What do I need to do to correct this problem? Sue <html>
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
9
by: Safalra | last post by:
The idea here is relatively simple: a java program (I'm using JDK1.4 if that makes a difference) that loads an HTML file, removes invalid characters (or replaces them in the case of common ones...
4
by: Roger Redford | last post by:
Dear Experts, I'm attempting to marry a system to an Oracle 817 datbase. Oracle is my specialty, the back end mainly, so I don't know much about java or javascript. The system uses javascript...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
3
by: Wayne Deleersnyder | last post by:
Hi All, I'm trying to create a function that will cause a pop-up alert to appear if dates which were chosen from a drop-down list were invalid on a page. There's 4 dates, so there's the...
9
by: Steve | last post by:
Hi; I've being going through some legacy code on an old JSP site I have been patching. I noticed that when I save the JSP down to my PC as an HTML file I get this javascript error in IE 6 ( ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.