Patchworkboy87 Patchworkboy87 - 1 year ago 43
HTML Question

Feed select option value with textbox/hidden input?

I am currently looking to have the value of the following (id="categoryid")...

<button class="btn btn-info btn-sm" onClick="MM_setTextOfTextfield('categoryid','','1')" data-toggle="modal" data-target="#AddNotesModal"><i class="fa fa-plus"></i> Add Notes
</button>

<input type="text" class="form-control" id="categoryid">


to feed into the option value below....

<select name="Category" required class="form-control">
<option value="">Select...</option>
<option value="______"><%=(rsNtsC.Fields.Item("NotesCategory").Value)%></option>


How would I go about doing this? I'm open to jQuery, AJAX and javascript solutions if necessary.

Many thanks

EDIT:
In my current set up the uses a value in a SQL database as an ID number, like so. I'm mostly looking to replace getting the value from the database with whatever the textbox has:

<select name="Category" required class="form-control">
<option value="">Select...</option>
<%
While (NOT rsNtsC.EOF)
%>
<option value="<%=(rsNtsC.Fields.Item("NotesCategoryID").Value)%>"><%=(rsNtsC.Fields.Item("NotesCategory").Value)%></option>
<%
rsNtsC.MoveNext()
Wend
If (rsNtsC.CursorType > 0) Then
rsNtsC.MoveFirst
Else
rsNtsC.Requery
End If
%>
</select>

Answer Source

If you are a looking for a way to include the value of the input into the select use the code below.

But remember that as soon as the page gets reloaded the values of the select will be gone.

function MM_setTextOfTextfield(input) {
  var count = $("select[name='Category'] option").length;
  $("select[name='Category']").append("<option val=\"" + count + "\">" + $("#" + input).val() + "</option>")
  $("#" + input).val("")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-info btn-sm" onClick="MM_setTextOfTextfield('categoryid')" data-toggle="modal" data-target="#AddNotesModal"><i class="fa fa-plus"></i> Add Notes
  </button>

<input type="text" class="form-control" id="categoryid">

<select name="Category" required class="form-control">
    <option value="">Select...</option>
</select>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download