Subject: populating drop down through text field
Posted By: nasirmunir Post Date: 6/4/2008 10:23:28 PM
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.

Reply By: vinod_yadav1919 Reply Date: 6/4/2008 11:33:02 PM
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
Reply By: nasirmunir Reply Date: 6/5/2008 8:18:56 AM
Thanks a million Vinod, that was perfect !!!

Reply By: nasirmunir Reply Date: 6/5/2008 8:43:03 AM
........another one: how do i set the defualt value for drop down to the text copied from the text box ?

Reply By: vinod_yadav1919 Reply Date: 6/5/2008 2:58:51 PM
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
Reply By: nasirmunir Reply Date: 6/30/2008 11:01:18 AM
Thanks Vinod !!!
It took me sometime to reply though, sorry for that :)


Go to topic 72416

Return to index page 1