473,387 Members | 1,423 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.

editable menu/list form

Is it possible to have an editable combobox (list/menu) form in HTML using javascript? it seems that this is not an standard widget in HTML. Should I create a new one or there is a better solution?
May 22 '09 #1
21 2637
acoder
16,027 Expert Mod 8TB
This is one attempt. The basic idea is to use a text box and a select element and place one on top of the other, so both can be used.
May 22 '09 #2
It seems that it is not an easy job... I am not expert in javascript. I have to work around with it.
May 22 '09 #3
acoder
16,027 Expert Mod 8TB
Well, in that case, the link I've posted should at least get you started.
May 22 '09 #4
After very long delay I am now here (!)
I tried to implement it and the class was easy to understand. One thing that I have not understand yet is passing the entered value to a variable.
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="">
  2. ...<select name="V" id="V" class="comboBox">
  3.       <option selected >Very low</option>
  4.       <option>Low</option>
  5.       <option>Medium</option>
  6.       <option>High</option>
  7.       <option>Very high</option>
  8. </select>
  9. ...
  10.  
now by clicking a button a function is called and I wrote:
Expand|Select|Wrap|Line Numbers
  1. var zz = document.form1.V.selectedIndex;
  2.     switch ( zz ) {
  3.         case 0:
  4.             var v = 0.1;
  5.             break;
  6.         case 1:
  7.             var v = 0.2;
  8.             break;
  9.         case 2:
  10.             var v = 0.3;
  11.             break;
  12.         case 3:
  13.             var v = 0.5;
  14.             break;
  15.         case 4:
  16.             var v = 1.5;
  17.             break;
  18.         default:
  19.             var v = document.form1.V.value;
  20.             alert( v );
  21. }
but it does not work. Anybody knows the problem? Unfortunately in the tutorial, it is not clear how to treat the entered text in the combobox.
Aug 3 '09 #5
gits
5,390 Expert Mod 4TB
how do you want to have the text treated?

kind regards
Aug 4 '09 #6
If the user wants, he can select one of the 5 items (very low, ..., very high) or he can write a float number. The default values for items are:
very low = 0.1
low = 0.2
medium = 0.3
high = 0.5
very high = 1.5

Now as a user, I want to enter 0.75. In the function I want the variable 'v' to get 0.75 and then manipulate it. For example var hc = 12.1 * Math.pow(v, 0.5);
Aug 4 '09 #7
gits
5,390 Expert Mod 4TB
so now ... correct me if i'm wrong ... but first i have to summarize that all:

you have an input type=text and a select node where the input 'overlays' the select. now you are able to type in the input element and/or drop down the select options where you could choose from ... right?

then now you just have to do the following to achieve your goal:

1. write a onchange handler for your select element that will write the choosen value to the input's value

2. now you just need to retrieve the value from the input ... when it was typed in then you would have that ... when someone selects from the dropdown it will be instantly set to the input - then do with that value whatever you want :)

the onchange handler could look like this (note the comments too):

Expand|Select|Wrap|Line Numbers
  1. function setInputValue() {
  2.     // retrieve the select's value -> you should add 
  3.     // the value props to the options
  4.     var val = document.getElementById('V').value;
  5.  
  6.     document.getElementById('yourInputIdHere').value = val;
  7. }
kind regards
Aug 4 '09 #8
The idea is right but some things are still vague.
1- what do you mean by "yourInputIdHere"? where should I define it?
2- when should I call "setInputValue()"?
3- is this syntax getElementById('V') correct? In my previous post, I wrote 'v' because the variable is a single character. The actual variable name is v as in Math.pow(v, 0.5);
Aug 4 '09 #9
gits
5,390 Expert Mod 4TB
1. the id-property of your input node

2. as i said: onchange of your select node

3. you already have:

Expand|Select|Wrap|Line Numbers
  1. <select name="V" id="V" class="comboBox">
and getElementById() retrieves elements by its id ... so the shown line:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('V');
  2.  
would be correct when you want to retrieve the above select node.
Aug 4 '09 #10
I have wrote this:
Expand|Select|Wrap|Line Numbers
  1.     <select name="V" id="V" class="comboBox" onchange="setInputValue(this.id)">
  2.       <option selected >Very low</option>
  3.       <option>Low</option>
  4.       <option>Medium</option>
  5.       <option>High</option>
  6.       <option>Very high</option>
  7.     </select>
then the function is (as you wrote):
Expand|Select|Wrap|Line Numbers
  1. <script language='javascript' type='text/javascript'>
  2. function setInputValue(x) {
  3.  
  4.  var val = document.getElementById(x).value;
  5.  document.getElementById(x).value = val;
  6. }
  7. </script>
  8.  
The new problem is I use the variable v in another button function. As I said I have a button, and for it I have a function:
Expand|Select|Wrap|Line Numbers
  1. <input name="C2" type="button" id="C2" value="Calculate" onclick="calc()" />
  2. ...
  3. <script language='javascript' type='text/javascript'>
  4. function calc() {
  5.     var zz = document.form1.V.selectedIndex;
  6.     switch ( zz ) {
  7.     ..........
  8.         .........
  9.     }
  10.  
No I can not use val in calc().
Sorry if it is messy, but I have no experience with handlers. I can post the entire code but it is a bit lengthy.
Aug 4 '09 #11
gits
5,390 Expert Mod 4TB
in the options set the value-attribute like this:

Expand|Select|Wrap|Line Numbers
  1. <option value="0.1">Low</option>
  2.  
then in your button's function you just need to use:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('yourInputIdhere').value
now. this retrieves the value of the input-node ... isn't that what you try to get there?

kind regards
Aug 5 '09 #12
I summarize what you said and wrote a sample file and used the combo/edit box to see if it work. Here is the code:
Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <body>
  3. <script type="text/javascript" src="comboBox.js"></script>
  4. <script language='javascript' type='text/javascript'>
  5. function calc() {
  6.     var v = document.getElementById('V').value;
  7.     //alert( v );
  8.     var z = Math.pow( v, 2 );
  9.     //alert( z );
  10.     document.form1.tem.value = z.toFixed(2);
  11. }
  12. </script>
  13. <script language='javascript' type='text/javascript'>
  14. function setInputValue(x) {
  15.  var val = document.getElementById(x).value;
  16.  document.getElementById(x).value = val;
  17.  alert ("input handler");
  18. }
  19. </script>
  20.  
  21. <form id="form1" name="form1" method="post" action="">
  22.     <select name="V" id="V" class="comboBox" onchange="setInputValue(this.id)">
  23.       <option value="0.1" selected >Very low</option>
  24.       <option value="0.2">Low</option>
  25.       <option value="0.3">Medium</option>
  26.       <option value="0.5">High</option>
  27.       <option value="1.5">Very high</option>
  28.     </select>
  29.     <input name="C2" type="button" id="C2" value="Calculate" onclick="calc()" />
  30. <input name="T" type="text" id="tem" size="10" maxlength="5" >
  31. </body>
  32. </html>
If you run the code, you will see that the calc() works fine for selected items, but it does not work for written values in the combo/edit box.

For example, if you select "very low" and press the button, the result will be 0.1^2=0.01
but if you enter 2 and press the button, the result will be 0.01 (!)

Although the onchange handler is present, I don't know why it is not aware of input changes. I know I have not correctly wrote the setInputValue, but I placed an alert inside the onchange handler to see if it is called, but it doesn't execute (!)

Thanks in advance...
Aug 5 '09 #13
gits
5,390 Expert Mod 4TB
you missed something :) ... in the calc-function use:

Expand|Select|Wrap|Line Numbers
  1. var v = document.getElementById('C2').value;
this should always be the correct value, because 'C2' is 'yourInputIdhere' while 'V' would be 'yourSelectIdhere' :)

kind regards
Aug 5 '09 #14
I am not so sure... because if you test with 'C2', the result will be "NaN".
Aug 5 '09 #15
gits
5,390 Expert Mod 4TB
i fixed it in the code below - i was wrong, the input id is 'temp' i overlooked it. note the changes :)

Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2.  
  3. <head>
  4. <script type="text/javascript">
  5.  
  6. function calc() {
  7.     var v = document.getElementById('tem').value;
  8.     var z = Math.pow( v, 2 );
  9.     document.form1.tem.value = z.toFixed(2);
  10. }
  11.  
  12. function setInputValue(x) {
  13.     var val = document.getElementById(x).value;
  14.     document.getElementById('tem').value = val;
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <form id="form1" name="form1" method="post" action="">
  20.     <select name="V" id="V" class="comboBox" onchange="setInputValue(this.id)">
  21.       <option value="0.1" selected >Very low</option>
  22.       <option value="0.2">Low</option>
  23.       <option value="0.3">Medium</option>
  24.       <option value="0.5">High</option>
  25.       <option value="1.5">Very high</option>
  26.     </select>
  27.  
  28.     <input name="C2" type="button" id="C2" value="Calculate" onclick="calc()" />
  29.     <input name="T" type="text" id="tem" size="10" maxlength="5" >
  30. </form>
  31. </body>
  32. </html>
Aug 5 '09 #16
You have removed the important line which is the core of combo/edit box:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="comboBox.js"></script>
removing that line causes the widget to be only combobox which I have worked with it before.
If you run run with comboBox.js and 'tem' as input handler, then the result will be always 0.00
Aug 6 '09 #17
gits
5,390 Expert Mod 4TB
with the current code it works correct ... so what is comboBox.js doing? the only thing that is missing is to overlay the input over the select node ... is that what it does? it shouldn't have anything to do with the calculation or does it? then i guess you should post ALL! relevant code instead of complaining that something is missing or working in another way ...
Aug 6 '09 #18
@gits
yes
@gits
I thought you were aware of comboBox.js. I have not written it. It is something that the original author of combo/edit box wrote and I only include it in my code. I have attached all relevant code.
Attached Files
File Type: zip combo-edit.zip (4.6 KB, 78 views)
Aug 6 '09 #19
gits
5,390 Expert Mod 4TB
nope ... i was not aware of that script. and it 'self-creates' the combo ... by appending a new input-node with an id 'txt'+'yourSelectNodeId' ... to make it work in this case the code could! look like:

Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2.  
  3. <head>
  4. <script src="comboBox.js"></script>
  5. <script type="text/javascript">
  6. function calc() {
  7.     var v = document.getElementById('txtV').value;
  8.  
  9.     if (isNaN(v)) {
  10.         document.form1.tem.value = '';
  11.         return;
  12.     }
  13.  
  14.     var z = Math.pow( v, 2 );
  15.     document.form1.tem.value = z.toFixed(2);
  16. }
  17.  
  18. </script>
  19. </head>
  20. <body>
  21. <form id="form1" name="form1" method="post" action="">
  22.     <select name="V" id="V" class="comboBox" onchange="setInputValue(this.id)">
  23.       <option value="0.1" selected >Very low</option>
  24.       <option value="0.2">Low</option>
  25.       <option value="0.3">Medium</option>
  26.       <option value="0.5">High</option>
  27.       <option value="1.5">Very high</option>
  28.     </select>
  29.  
  30.     <input name="C2" type="button" id="C2" value="Calculate" onclick="calc()" />
  31.     <input name="T" type="text" id="tem" size="10" maxlength="5" >
  32. </form>
  33. </body>
  34. </html>
  35.  
the setting of the value is even 'self-handled' ... and so you just need to retrieve the correct value from the appended input-node.
Aug 6 '09 #20
great... it is now working. Indeed it was very tricky...
Thanks
Aug 6 '09 #21
gits
5,390 Expert Mod 4TB
:) ... not tricky at all. as you might have noticed we currently nearly rebuilt the comboBox.js and you could have seen how that basicly works. and basicly it is a very simple script.

but i'm glad to hear that it works now for you ... :)

kind regards
Aug 6 '09 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oliver Hoehle | last post by:
Hello! This ist the source-code for an editable combobox implemented with HTML,CSS and Javascript. I have tested it with IE and Mozilla. But I don't know, if it will work in other browsers...
0
by: Amiram Korach | last post by:
When you create a MDI form, you can attach a main menu to the parent and to the child. When a child form is active, its menu is merged with the parent menu. The problem is: when the forms are...
1
by: Joe Spears | last post by:
Hi I want to display a datagrid summary of a datatable. The grid will not show all the fields, but when the user selects a row, I want the other controls (text boxes) to also bind to the same row...
4
by: Henry | last post by:
Hi all, I would like to use a control on a WebForm that acts like an editable DropDownList control. The user should be able to select a value from the list of choices, or type in a new value if...
2
by: Ravi Joshi | last post by:
The menu on my site works fine in IE6 and Firefox. In IE7, there is a problem with the menu: when you mouse over the various main catagories, the sub-catagories all appear to the right as they...
2
by: namratha247 | last post by:
Hi, Can anybody please help me with this problem. In my application, I'm using Menus. and the CSS for the menu is as given below ccMenu.css /* Root = Horizontal, Secondary = Vertical */...
4
by: Paul | last post by:
Hi all, I have a page that has a form on it which has a dropdown list on it. It connect to an sql database and populate the list. What I would like to do is make the list editable so that if the...
3
by: tceramesh | last post by:
Hi Friends, I Created one drop down menu by using CSS. It is perfect in Firefox but in IE it creates some problem. in the header section the drop down menu appear,, below that...
1
akashazad
by: akashazad | last post by:
Dear Friends In my Form I have the Tree View in which I want to Add the Node during the Run time. For that I used a Context Menu Strip and on the Click of the Pop menu I added the new...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.