• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Dynamically adding select options with html entities

It's fairly easy to add option elements to a select:

select = document.getElementById('MySelect');
opt = document.createElement('option');
opt.text = "New Option";
opt.value = "New Option";
try {
    select.add(opt, null);
} catch(ex) {
    select.add(opt);
}

But, how does one add elements which include html entities.  For instance:

opt.text = "£20";

0
phasevar
Asked:
phasevar
  • 3
1 Solution
 
smaccariCommented:
Try with innerHTML of the option:

<select id=myselect>
</select>
<script>
document.getElementById("myselect").options[1]=new Option("","");
document.getElementById("myselect").options[1].innerHTML ="&pound;20";
</script>
0
 
Michel PlungjanIT ExpertCommented:
ot this:
<select id="MySelect"></select>
<script>
pnd = unescape("%A3")
select = document.getElementById('MySelect');
opt = document.createElement('option');
opt.text = pnd+20;
opt.value = 20;
try {
    select.add(opt, null);
} catch(ex) {
    select.add(opt);
}
</script>
0
 
Michel PlungjanIT ExpertCommented:
No split?
0
 
phasevarAuthor Commented:
mplungjan, for some reason when I adapted your code to my application it didn't work in Firefox (or was it IE?  I can't remember now).   However, I just copied and pasted your example and it seems to work in both.  So maybe it was my error.  Sorry.  I guess it's too late to split.  I don't see the option.
0
 
Michel PlungjanIT ExpertCommented:
Yes it is too late. But that is ok. Just curious since my suggestion should work in the more standard way of creating options too.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now