473,785 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="obscu re()" 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 3662
deepee wrote:
I have a drop down box in HTML using SELECT and OPTION tags:

<select title="Choose a number" onchange="obscu re()" 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="obscu re()" with onchange="obscu re(this)"
and then:
function obscure(sel) {
sel.options[sel.selectedInd ex].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="javas cript">
function obscure(chosenN um){
for(i=0;i<docum ent.all('dropdo wn1').length;i+ +)
{
if(document.all ('dropdown1').o ptions[i].value== '*')
{
document.all('d ropdown1').remo ve(i);

}
}

select = document.getEle mentById('dropd own1');
opt = document.create Element('option ');
opt.text = "*";
opt.value = "*";
try {
select.add(opt, null);
} catch(ex) {
select.add(opt) ;
}

for(i=0;i<docum ent.all('dropdo wn1').length;i+ +)
{
if(document.all ('dropdown1').o ptions[i].value== '*')
{
document.all('d ropdown1').sele ctedIndex=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="javas cript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function obscure(chosenN um){
Where does 'chosenNum' come from? What is its value? Why is it never used?

for(i=0;i<docum ent.all('dropdo wn1').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').o ptions[i].value== '*')
{
document.all('d ropdown1').remo ve(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="obscu re(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.getEle mentById('dropd own1');
opt = document.create Element('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<docum ent.all('dropdo wn1').length;i+ +)
{
if(document.all ('dropdown1').o ptions[i].value== '*')
{
document.all('d ropdown1').sele ctedIndex=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.c om/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.selectedInd ex].text = "*"; }
</script>

<table>
<tr>
<td>
<SELECT TITLE="Choose a number" onchange="obscu re(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.selectedInd ex=len;
//alert("chosenNu m = " + chosenNum);
}
</script>

<table>
<tr>
<td>
<SELECT TITLE="Choose a number" onMouseDown="re moveStar(this)"
onchange="obscu re(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
3212
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 layer behind the menus. The closer div has a lower index than the submenu divs so it appears behind them. The closer div contains a transparent gif with an event applied to it to close all of the divs when moused over.
19
6932
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 JavaScript in this code from a couple different sites but I'm not 100% sure what each line is doing...This is the ASP code that I'm using for the page....Take a look at the JavaScript code and please let me know what each line is doing....I have been...
2
4310
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 = False (the message at runtime is something like index out of range, and under the debuger, I discover that the attribute count of the columns collection is 0!) Below is my piece of code.
2
3098
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 where when a user selects an item from a dropdown box and attempts to run a report, if a date is not found in my database, it should return a message to the browser notifying a user that they must select another item to run the report against. The...
3
2911
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 how to set the checked state of a checked listbox item in code. Here is the drag/drop code, which works fine, except for unchecking the dropped item: Private Sub clbQueryItems_DragDrop(ByVal sender As System.Object, ByVal e As...
2
1632
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 from the table, so that the user only is allowed to access the procedure. I also tried using a SQL function, but this doesn't work, too. Working with views may be a solution - or are e.g. cursors
5
2178
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</option> <option value="1" selected >Connected Unit 2</option> </select> <span onclick="NewWindow('pluginpage.html?EditUnitGUID='+this.form.PlantUnitID.options.value+'&DoPostBack=1','namexx',200,310);"
1
4623
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 can give each optgroup individual ID, e.g. id="group1"..."group"17). e.g. the html code looks like: <select name="mySelect" id="mySelect"> <optgroup label="First group" id="group1" > <option value="a" > Item A
22
12495
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
1
2371
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 one.i know how to show/hide a row when its not dynamic.but i dont know how to do it when its logic iterate tag. <logic:iterate indexId="index" id="phoneItem" name="nameForm" property="results"> <% String dropdownType = "results.dropdownType";...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.