Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2013, 05:09 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Adding a value from an inputbox into combo box

Hello,

I need your guys help,

Using javascript,

What i'd like to do is when I type a value into the inputbox and I hit my enter key (in the textbox only) i'd like to have the value of the inputbox be added into the drop down box.

Note that if there is an existing value in the already in the drop down box, the next value should just be added below the existing value.

How do I set this up?

Here is the html setup:

Code:
<!DOCTYPE html>

<html>

<head>

</head>

<body>

<input type="text" id="theinput" style="width: 100px;">

<select id="thebox" style="width: 100px;"></select>

</body>

</html>
Much thanks and appreciation for all your help and support.

Cheers,

Jay
jason_kelly is offline   Reply With Quote
Old 01-24-2013, 05:13 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Google "javascript new option" (and be sure to use the additional search tools to specify "within last year").

UPDATE: This tutorial is helpful.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 01-24-2013 at 05:18 PM..
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
jason_kelly (01-24-2013)
Old 01-24-2013, 06:01 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are:-

Code:
<form id = "myform">
<input type = "text" onblur = "addOption(this.value)">

<select name = "listBox1" id = "listBox1">
<option value = "">First Value If Desired</option>
</select>
</form>

<script type = "text/javascript">

function addOption(txt) {
var s = document.getElementById('listBox1');
var opt = new Option(txt);

for (var i = 0; i<s.options.length; i++) {
var xtext = document.getElementById("myform").listBox1[i].text; 
if (txt == xtext) {
alert ("You have already added this value to the select box")
return false;
}
}
s.options[s.options.length] = opt;
return true;
} 

</script>
You should know that the default behaviour of hitting the Enter key is to transfer the focus to the next following field.


Samson slayed the Philistines with the axe of the apostles
- Pupil's answer to Catholic Elementary School test.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
jason_kelly (01-24-2013)
Old 01-24-2013, 07:44 PM   PM User | #4
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
Thanks very much Phil.

Flawless
jason_kelly is offline   Reply With Quote
Old 01-24-2013, 09:13 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Note that a select list is NOT a combo box. With a combo box the person doesn't have to select a value from the list, they can type in their own value instead. Combo boxes are available in HTML 5.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-24-2013, 10:05 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I've been preaching "<select> is not a combo box" for at least 14 years. It hasn't helped. And of course now that HTML 5 really has combo boxes, we'll never manage to get people to keep them straight.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:02 AM.


Advertisement
Log in to turn off these ads.