• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

new option() and &

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when populating the option object dynamically, I am not getting the expected result when the string contains &

string="This is Testing -> A & B ";
select = window.document.search.mySelect;
select.options.length = 0;
select.options[0]= new Option(string);

Prints option contains ->"This is Testing -> A & B ";
Not "This is Testing -> A & B "

Where as if I create it this way
<select name="mySelect">
<option>Apple &</option>
</select>

The reslting string is - > Apple &


Help.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you use &amp; in the html page, the html renders it as the correct character which you are seeing when you har code it into the page. The problem with it when JavaScript adds it, it doenot go through that rendering and it displays as the string.

The only option I can think of is to create a function that does a find and replace when adding the string. This way you can display the correct characters. One other problem that the & character brings to the plate is in the query string if you are submitting the form that way.

Eric
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric.
In the database, I have the data as 'Business & Unit'
And I am sending a list of bean objects to my JSP and I am iterating throught <html:iterate> tag to pupulate the options dynamicaly in the JSP.

When the <bean:write> tag prints the above data, its printing as
'Business & Unit'
I was expecting it to print as 'Business & Unit'

I am not aware how the bean tag prints the results. Is there a way I can make the data print the way it fetches it from database.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the basic idea I came up with:



Eric
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked, except when there are two occurances of '&' in the string.
It only replaces once.

regExp = /&/gi;
function DoReplace(xString){
return xString.replace('&','&');
}
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked, except when there are two occurrences of '&' in the string.
It only replaces once.

regExp = /&/gi;
function DoReplace(xString){
return xString.replace('&','&');
}

How do search till the end of the sentence and replace all occurrences of & with &.

Also I did not understand /&/gi;
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\'&\' with &
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regExp = /&amp;/gi;

That should replace it multiple times. It is a regular expression....
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, I wanted to post

replace & a m p; &
 
James Workmen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you soo much Eric. That worked.
I am very greatful to you :-)
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was harder then it should be with the forum changing the &amp; to the & sign. I did not think about that when I posted my solution at first!

lol

Glad I could help!

Eric
[ November 18, 2004: Message edited by: Eric Pascarello ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic