473,387 Members | 1,798 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,387 software developers and data experts.

Hide select item in a drop-down box with an Asterisk

I have a drop down box in HTML using SELECT and OPTION tags:

<select title="Choose a number" onchange="obscure()" name="Digit1"
ID="Digit1">
<OPTION VALUE="">&nbsp;</OPTION>
<OPTION VALUE="0">&nbsp;0</OPTION>
<OPTION VALUE="1">&nbsp;1</OPTION>
<OPTION VALUE="2">&nbsp;2</OPTION>
<OPTION VALUE="3">&nbsp;3</OPTION>
</select>

Using the 'onchange' event I can fire a javascript function obscure().
How can I hide the selected number with an Asterisk (like a password)?
I've tried several different Googles but I can't find a method that
works.

Anyone have any ideas?

Thanks in advance

Apr 25 '06 #1
4 3630
deepee wrote:
I have a drop down box in HTML using SELECT and OPTION tags:

<select title="Choose a number" onchange="obscure()" name="Digit1"
ID="Digit1">
<OPTION VALUE="">&nbsp;</OPTION>
<OPTION VALUE="0">&nbsp;0</OPTION>
<OPTION VALUE="1">&nbsp;1</OPTION>
<OPTION VALUE="2">&nbsp;2</OPTION>
<OPTION VALUE="3">&nbsp;3</OPTION>
</select>

Using the 'onchange' event I can fire a javascript function obscure().
How can I hide the selected number with an Asterisk (like a password)?
I've tried several different Googles but I can't find a method that
works.


Looks awfully dicey on the example you gave - hopefully you have a good
reason.
Here's one approach:
Replace onchange="obscure()" with onchange="obscure(this)"
and then:
function obscure(sel) {
sel.options[sel.selectedIndex].text = "*"; }

Csaba Gabor from Vienna

Apr 25 '06 #2
Thanks for the reply.

Not sure why you think it's dicey to do what I'm doing. I just want to
obscure the selected number. Much in the same way that a Password field
does.

Sorry to say your code didn't work, though i'm not sure why.

I've come up with this but isn't exactly tidy and leaves an asterisk in
the list.

<script language="javascript">
function obscure(chosenNum){
for(i=0;i<document.all('dropdown1').length;i++)
{
if(document.all('dropdown1').options[i].value== '*')
{
document.all('dropdown1').remove(i);

}
}

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

for(i=0;i<document.all('dropdown1').length;i++)
{
if(document.all('dropdown1').options[i].value== '*')
{
document.all('dropdown1').selectedIndex=i

}
}

}

</script>

Apr 25 '06 #3
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:

<script type="text/javascript">

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[i].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[i];
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[i];
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[i].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).

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 25 '06 #4
Rob, thanks for your time and the comprehensive reply.

Some of my intention obviously wasn't clear so I'll try and clarify.

- I want a drop-down box that users can select from but the selection
they make must then be obscured so that it can't be seen by others once
selected. I know the user cannot see what they have selected after
they've made the selection, that's the intention. I want the drop-down
to work in the same way as a password field. You can't see what you
have typed in these, I want the drop-down equivalent.

- Csabas code didn't work because it replaced the selected number with
an asterisk rather than obscuring it.

........
function obscure(sel) {
sel.options[sel.selectedIndex].text = "*"; }
</script>

<table>
<tr>
<td>
<SELECT TITLE="Choose a number" onchange="obscure(this)"
NAME="dropdown1" ID="dropdown1">
.........

- the code i posted was a stab at a solution, cobbled together from
various parts found on the net, to give an idea of my intentions, it
seems to have muddied the waters - apologies

I've taken your suggestions and hopefully come up with a reasonable
piece of code. Happy for any other pointers though. In an ideal world
I'd just like to display an asterisk without having to add/remove it
each time but I've yet to find a way of doing this.

Thanks
Dean

<html>
<body>

<script type="text/javascript">
function removeStar(sel){
var opt, opts = sel.options;
var i = opts.length;
while (i--){
opt = opts[i];
if ('*' == opt.value){
sel.remove(i);
}
}
}

function obscure(sel)
{
var chosenNum = sel.value;
var len = sel.length;
sel.options[len] = new Option("*","*");
sel.selectedIndex=len;
//alert("chosenNum = " + chosenNum);
}
</script>

<table>
<tr>
<td>
<SELECT TITLE="Choose a number" onMouseDown="removeStar(this)"
onchange="obscure(this)" NAME="dropdown1" ID="dropdown1">
<OPTION VALUE="">&nbsp;</OPTION>
<OPTION VALUE="0">&nbsp;0</OPTION>
<OPTION VALUE="1">&nbsp;1</OPTION>
<OPTION VALUE="2">&nbsp;2</OPTION>
<OPTION VALUE="3">&nbsp;3</OPTION>
<OPTION VALUE="4">&nbsp;4</OPTION>
<OPTION VALUE="5">&nbsp;5</OPTION>
<OPTION VALUE="6">&nbsp;6</OPTION>
<OPTION VALUE="7">&nbsp;7</OPTION>
<OPTION VALUE="8">&nbsp;8</OPTION>
<OPTION VALUE="9">&nbsp;9</OPTION>
</SELECT>

</TD>
</tr>
</table>

</body>
</html>

Apr 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David | last post by:
Hi everyone, Hoping there are some .js/browser experts out there that can help with this weird problem. I have made a swap div routine and applied the events to menu buttons with a closer...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
2
by: Thanh Nu | last post by:
Hi, I would like to hide a column in a web datagrid (with create columns automatically at runtime checked), and I cannot refer to the columns collection like this: DataGrid1.Columns(0).Visible...
2
by: J.B. | last post by:
Greetings all - I have been working on this issue for a while now and it has been frustrating to the point where I'm reaching out to the gurus of .NET for any suggestions. I have a webform...
3
by: Dean Slindee | last post by:
In a checked listbox, I am allowing drag/drop of the items within (resequencing). Problem is, when dropping a checked item, the checked state always reverts to unchecked (unwanted). Anyone know...
2
by: Gellert, Andre | last post by:
Hello, I have following problem: A user "xy" shouldn't have any rights to a table, but needs data from the content of the table. My idea was to setup a PL/PGSQL procedure to fetch the data...
5
by: GTi | last post by:
Whats wring with this code? <select class=EditField size="1" name="PlantUnitID" title="Select line"> <option value="0" >Standalone Unit</option> <option value="1" selected >Connected Unit...
1
by: TKapler | last post by:
I think i am quite experienced javascript programmer, but I got a problem. I have a selectbox with e.g. 17 optgroups with 100 options. I need a javascript code to hide some of that optgroups (i...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: prathna | last post by:
Hi .. I have a logic:iterate tag which will display 5 rows each row with a drop downlist and 2 textfields.now by default all the rows will be shown.how do i hide all the rows except the first...
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: 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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.