Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2008, 10:23 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default populating drop down through text field

Hi,
Is there a way of populating a drop down list through a text field ? I am working on a project where a user can type in a text field. On enter or click the typed message should be copied to the drop down list and removed from the text field. I have seen some kind of solution from drop down to text field but not the other way around.
Can I get some directions here? thanks.

 
Old June 4th, 2008, 11:33 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Nasir!!
Do you want to add these textbox values to dropdown from the client side??
e.g there is a text box ,dropdown and button.
user enters some name and clicks on the button,now only on the client side do you want to appear to the dropdown??(without post back to the server).

Then again its depends on technology(asp,php,or .NET) and the requirements/implementations too!!

Following code will give some directions on it
<script>
function addToDropdown()
{
//put your validation here before attaching to dropdown

txtName=document.getElementById('txtbox').value;
txtValue=txtName //if you want diff value put ur validation here
obj=document.getElementById("ddbox");

obj.options[obj.options.length] = new Option(txtName,txtValue)

obj.selectedIndex=obj.options.length-1;

}


</script>
<select name="ddbox" id="ddbox"></select>
<input type=text id="txtbox" >
<input type=button onclick="addToDropdown()" value="add To Dropdown">

to remove selected dropdown value/text
obj.options[obj.selectedIndex] = null;


Cheers :)

vinod
 
Old June 5th, 2008, 08:18 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks a million Vinod, that was perfect !!!

 
Old June 5th, 2008, 08:43 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

........another one: how do i set the defualt value for drop down to the text copied from the text box ?

 
Old June 5th, 2008, 02:58 PM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Nasir!!
dropdown has two thing, text and value
e.g.
<option value='x'>mytext</option>
in my previous post, i have put the default value of text as a default text..

txtName="mytext"
txtValue="x"
obj.options[obj.options.length] = new Option(txtName,txtValue)

//i have added below code so that everytime you enter the text and click on the button,newly added option should be default selected.
//if you comment below code you will find new option is added .
obj.selectedIndex=obj.options.length-1;


or I think you want to set the value of select box e.g. below code set the select box value to 2..
******test.html*******
<select id="sel">
<option value=1>VAl1</option>
<option value=2>VAL102</option>
</select>
<script>
obj=document.getElementById("sel")
obj.value=2
</script>
**********************
If this doesn't give any pointers to your question then could you explore more on ur requirement?



Cheers :)

vinod
 
Old June 30th, 2008, 11:01 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Vinod !!!
It took me sometime to reply though, sorry for that :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating a drop down list from an ArrayList snejsnej ASP.NET 1.0 and 1.1 Basics 1 April 20th, 2006 11:14 AM
show/hide text field from select (drop down) Gotaka4 Javascript 6 December 7th, 2005 05:44 AM
populating a text field sand133 VB.NET 2002/2003 Basics 12 June 18th, 2004 08:49 AM
drop down is not populating spm74 ASP.NET 1.0 and 1.1 Basics 4 March 16th, 2004 12:36 PM
Populating Drop Down based on other drop down jeffbarclay Java Databases 1 November 7th, 2003 12:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.