/    Sign up×
Community /Pin to ProfileBookmark

How to combine a User input field and a SelectBox?

I am having a lot of trouble with getting this to work. basically i have a selectbox. if a user clicks on anything in that box it moves it from the left box to the right box. simple

now what i need to do is combine it with user input. basically if user inputs 1 then i need it to put the first option from the select box from left to right just as if it was clicked while leaving the ability to click the rest… in the long run it will be pulling from a variable and if that variable is blank then it will still have the functionality to choose it manually from the list.

[code]
<HEAD>

<SCRIPT LANGUAGE=”JavaScript”>

<!– Begin
function moveOver()
{
if (document.choiceForm.manual.value == “1”){
var boxlength = “1”;
var selectedItem = “1”;
var selectedText = document.choiceForm.available.options[selectedItem].text;
var selectedValue = document.choiceForm.manual.value;
}
else {
var boxLength = document.choiceForm.choiceBox.length;
var selectedItem = document.choiceForm.available.selectedIndex;
var selectedText = document.choiceForm.available.options[selectedItem].text;
var selectedValue = document.choiceForm.available.options[selectedItem].value;
}

var i;
var isNew = true;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (document.choiceForm.manual.value == “1”){
thisitem = document.choiceForm.choiceBox.options[1].text;
}
else {
thisitem = document.choiceForm.choiceBox.options[i].text;}
if (thisitem == selectedText) {
isNew = false;
break;
}
}
}
if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
document.choiceForm.choiceBox.options[boxLength] = newoption;
}
document.choiceForm.available.selectedIndex=-1;
}
function removeMe() {
var boxLength = document.choiceForm.choiceBox.length;
arrSelected = new Array();
var count = 0;
for (i = 0; i < boxLength; i++) {
if (document.choiceForm.choiceBox.options[i].selected) {
arrSelected[count] = document.choiceForm.choiceBox.options[i].value;
}
count++;
}
var x;
for (i = 0; i < boxLength; i++) {
for (x = 0; x < arrSelected.length; x++) {
if (document.choiceForm.choiceBox.options[i].value == arrSelected[x]) {
document.choiceForm.choiceBox.options[i] = null;
}
}
boxLength = document.choiceForm.choiceBox.length;
}
}
function saveMe() {
var strValues = “”;
var boxLength = document.choiceForm.choiceBox.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i < boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.choiceBox.options[i].value;
}
else {
strValues = strValues + “,” + document.choiceForm.choiceBox.options[i].value;
}
count++;
}
}
if (strValues.length == 0) {
alert(“You have not made any selections”);
}
else {
alert(“Here are the values you’ve selected:rn” + strValues);
}
}

// End –>
</script>

</HEAD>

<!– STEP TWO: Copy this code into the BODY of your HTML document –>

<BODY>

<center>
<form name=”choiceForm”>
<input type=”text” name=”manual” value=”1″ id=”manual”>
<table border=0>
<tr>
<td valign=”top” width=175>
Available Content:
<br>
<select name=”available” size=10 onchange=”moveOver();”>
<option value=1>Company News
<option value=2>Industry News
<option value=3>Product Updates
</select>
</td>
<td valign=”top”>
Your Choices:
<br>
<select multiple name=”choiceBox” style=”width:150;” size=”10″>
</select>
</td>
</tr>
<tr>
<td colspan=2 height=10>
<input type=”button” value=”Remove” onclick=”removeMe();”>
<input type=”button” value=”move” onclick=”moveOver();”>
<input type=”button” value=”Get Selected Values” onclick=”saveMe();”>
</td>
</tr>
</table>

</form>
</center>[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@pentaceauthorMar 06.2007 — Sorry I was not able to edit my last post for some reason the button is missing.


Here is the updated code i have it working 95%

basically the only issue i have now is i need all the user inputs to copy over at one time not one by one like it does not if you hit the insert button.. can nayone help?

<i>
</i>

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;

&lt;!-- Begin
function moveOver() <br/>
{
var boxLength = document.choiceForm.choiceBox.length;
var selectedItem = document.choiceForm.available.selectedIndex;
var selectedText = document.choiceForm.available.options[selectedItem].text;
var selectedValue = document.choiceForm.available.options[selectedItem].value;
var i;
var isNew = true;
if (boxLength != 0) {
for (i = 0; i &lt; boxLength; i++) {
thisitem = document.choiceForm.choiceBox.options[i].text;
if (thisitem == selectedText) {
isNew = false;
break;
}
}
}
if (isNew) {
newoption = new Option(selectedText, selectedValue, false, false);
document.choiceForm.choiceBox.options[boxLength] = newoption;
}
document.choiceForm.available.selectedIndex=-1;
}
function removeMe() {
var boxLength = document.choiceForm.choiceBox.length;
arrSelected = new Array();
var count = 0;
for (i = 0; i &lt; boxLength; i++) {
if (document.choiceForm.choiceBox.options[i].selected) {
arrSelected[count] = document.choiceForm.choiceBox.options[i].value;
}
count++;
}
var x;
for (i = 0; i &lt; boxLength; i++) {
for (x = 0; x &lt; arrSelected.length; x++) {
if (document.choiceForm.choiceBox.options[i].value == arrSelected[x]) {
document.choiceForm.choiceBox.options[i] = null;
}
}
boxLength = document.choiceForm.choiceBox.length;
}
}
function saveMe() {
var strValues = "";
var boxLength = document.choiceForm.choiceBox.length;
var count = 0;
if (boxLength != 0) {
for (i = 0; i &lt; boxLength; i++) {
if (count == 0) {
strValues = document.choiceForm.choiceBox.options[i].value;
}
else {
strValues = strValues + "," + document.choiceForm.choiceBox.options[i].value;
}
count++;
}
}
if (strValues.length == 0) {
alert("You have not made any selections");
}
else {
alert("Here are the values you've selected:rn" + strValues);
}
}



// End --&gt;
&lt;/script&gt;

&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document --&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;form name="choiceForm"&gt;
&lt;table border=0&gt;
&lt;tr&gt;
&lt;td valign="top" width=175&gt;
Available Content:
&lt;br&gt;
&lt;select name="available" size=10 onchange="moveOver();"&gt;
&lt;option value=1&gt;Company News
&lt;option value=2&gt;Industry News
&lt;option value=3&gt;Product Updates
&lt;/select&gt;
&lt;/td&gt;
&lt;td valign="top"&gt;
Your Choices:
&lt;br&gt;
&lt;select multiple name="choiceBox" style="width:150;" size="10"&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan=2 height=10&gt;
&lt;input type="button" value="Remove" onclick="removeMe();"&gt;
&lt;input type="button" value="Get Selected Values" onclick="saveMe();"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;/form&gt;
&lt;/center&gt;

&lt;SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"&gt;
function insertNewOption()
{
myOption = new Option();
myOption.text = document.selectionForm.textinsert.value;
myOption.value = document.selectionForm.valueinsert.value;
if (document.choiceForm.choiceBox.selectedIndex &gt; 0)
insertIndex = document.choiceForm.choiceBox.selectedIndex;
else
insertIndex = document.choiceForm.choiceBox.options.length;

document.choiceForm.choiceBox.options[insertIndex] = myOption;

if (insertIndex == '1')

myOption.text = document.selectionForm.textinsert1.value;
myOption.value = myOption.text;


if (insertIndex == '2')

myOption.text = document.selectionForm.textinsert2.value;
myOption.value = myOption.text;


if (insertIndex &gt;= '3')
insertIndex=null;

}

&lt;/SCRIPT&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form name="selectionForm"&gt;
manual input:
&lt;input type="text" value="Company News" name="textinsert" size="20"&gt;
&lt;input type="hidden" value="1" name="valueinsert" size="10"&gt;&lt;br&gt;
manual input:
&lt;input type="text" value="Industry News" name="textinsert1" size="20"&gt;
&lt;input type="hidden" value="2" name="valueinsert1" size="10"&gt;&lt;br&gt;
manual input:
&lt;input type="text" value="Product Updates" name="textinsert2" size="20"&gt;
&lt;input type="hidden" value="3" name="valueinsert2" size="10"&gt;
&lt;input type="button" value="Insert new option" onclick="insertNewOption();"&gt;
&lt;/form&gt;
×

Success!

Help @pentace spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...