deepee said on 25/04/2006 9:52 PM AEST:
Thanks for the reply.
Not sure why you think it's dicey to do what I'm doing. I just want to
Probably because once a user selects an option they can no longer see
the value they selected.
obscure the selected number. Much in the same way that a Password field
does.
Csaba's code does exactly that.
Sorry to say your code didn't work, though i'm not sure why.
'Didn't work' how? What error message? What did your modified code look
like?
I've come up with this but isn't exactly tidy and leaves an asterisk in
the list.
It is IE-specific and uses some bad coding practices. It also does
something completely different to that specified in your first post.
You asked for a function that replaced the text of the selected option
with an asterisk '*'.
The function you have posted will replace the text of any option with a
value of '*' with '*' in some browsers. It does so very inefficiently
and almost without regard for standards.
<script language="javascript">
The language attribute is deprecated, type is required:
function obscure(chosenNum){
Where does 'chosenNum' come from? What is its value? Why is it never used?
for(i=0;i<document.all('dropdown1').length;i++)
'i' will be global, it is usually important to keep counters local so
use 'var'.
The use of 'document.all' will stop this from working in a large number
of browsers, it very likely won't work in Gecko-based browsers. Csaba
suggested having the onchange handler pass a reference to the select to
the function, then you don't need to find the select later (making your
code is more portable and efficient).
The way you've written it, on every loop the statement must find
'dropdown1' and get its length property. It is much more efficient to
get that only once.
{
if(document.all('dropdown1').options.value== '*')
{
document.all('dropdown1').remove(i);
If you want to remove the options with a value of '*', then do the
following:
Pass a reference to the select from the change event:
<select onchange="obscure(this);" ...>
Change the function to:
function obscure(sel)
{
var opt, opts = sel.options;
var i = opts.length;
while (i--){
opt = opts;
if ('*' == opt.value){
sel.remove(i);
}
}
}
The 'while' loop counts backwards through the options, which can be handy.
}
}
select = document.getElementById('dropdown1');
opt = document.createElement('option');
opt.text = "*";
opt.value = "*";
That is silly - you remove the option, then replace it with one that has
different text? Why not just replace the text of the existing option?
The above while loop becomes:
while (i--){
opt = opts;
if ('*' == opt.value){
opt.text = '*';
}
}
Incidentally, it is much more reliable to add options using new
Option(), search the archives for examples.
try {
select.add(opt, null);
} catch(ex) {
select.add(opt);
}
If ever you are tempted to use try..catch, you are probably doing
something wrong. There is rarely any need for it (though it is handy in
a few limited cases).
for(i=0;i<document.all('dropdown1').length;i++)
{
if(document.all('dropdown1').options.value== '*')
{
document.all('dropdown1').selectedIndex=i
This will successively select all the options with a value of '*',
probably leaving the last one as selected, which may not be the one that
the user actually selected. Dicey indeed.
The logic of what you are trying to do doesn't make sense, I hope it
does to you (and your users).