Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handling the "&" character in a string

Status
Not open for further replies.

karen4201

Programmer
Mar 9, 2006
37
US
Hi all,

I'm trying to build 2 drop-down lists. When the first (list A) drop-down is selected and an item is picked, the JS will automatically populate the second (list B) drop-down list. I have it working for most items. However, if list A contains an item with a "&" character, such as "copy&paste", then list B does not automatically get processed by the JS. Maybe the JS isn't recognizing the characters.

here is some of list A:
<option value="copy\&amp;paste" >copy&amp;paste</option>

here is some of the JS for doing the auto populating of list B:

if (categoryList[categoryList.selectedIndex].value == "copy\&amp;paste") {AddToOptionList(document.formCreate.create_type, "do something", "do something");
AddToOptionList(document.formCreate.create_type, "do something else", "do something else");
AddToOptionList(document.formCreate.create_type, "do both", "do both");
}

note: I made "&" become "\&" because I read that JS doesn't like the "&" character by itself. And I made "&" become "&amp;" so that it is good html for a web browser.


Any ideas why this isn't working?

Thank you in advance.
 
note: I made "&" become "\&" because I read that JS doesn't like the "&" character by itself. And I made "&" become "&amp;" so that it is good html for a web browser.

As long as the & is in a string (which it is in your case) then it shouldn't make a difference. Have you tried taking the \ out to see if that works?

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
I've never heard that Javascript has a problem with an ampersand in a string. I know it's a bitwise AND operator, and 2 of them together are a logical AND, but that's not related to strings unless you put the string inside eval().

Other than that, you're not using the options array in checking if the value is copy&amp;paste

Code:
if (categoryList[b][red].options[/red][/b][categoryList.selectedIndex].value == "copy\&amp;paste")
 
Thanks for the tips. I figured out what was going on. It seems that the browser (IE 6.0) will accept "copy&amp;paste" and display it properly. However, when I do the dropdown selection, the JS does a string comparison as though the value was passed as an html string. So the "copy\&amp;paste" becomes "copy&paste". Now that I'm doing the adjusted string comparison, it's working!

Thanks again for the help. You all pointed me in the right direction :)
 
correction: I meant to type....


So the "copy&amp;paste" becomes "copy&paste". Now that I'm doing the adjusted string comparison, it's working!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top