Select first element of dropdown of an input box - javascript

I have an input box
<input id="source" onchange="openDropdown(...)" >
Whenever I enter any text inside this input box a dropdown of suggestion appears. Now I want to automatically select the first suggestion using JavaScript, but can't find any way to do this.
REQUIREMENT: You need to only use input id to select the first suggestion. I don't know the onchange function.
NOTE:- I am not having any select element here.I am just having an
input element here.So the thing is that i just want to select the
first element in the dropdown which will open after we start typing in
that input box(& i don't know how this dropdown is coming.I just know
that some input box is there.)I am not having the full code.

try this code,By default I select option 3 but you can change it
function openDropdown(){
$("#lst option:first").prop("selected","selected")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="source" onchange="openDropdown()" >
<select id="lst">
<option value=1>option 1</option>
<option value=2>option 2</option>
<option value=3 selected>option 3</option>
<option value=4>option 4</option>
</select>

$("#source") .change(function () {
var abc = $('option:first-child',this) .text();
Please try this .Hope this will help You..

Related

How to collect selected dropdown text with Javascript DOM?

Using Bootstrap 4 example html below, how can I extract the text from selected dropdown menu option with JS DOM or Jquery?
<select class="custom-select" id="inputGroupSelect01">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I know that I can fetch the value but not the text within <option></option> with something like this:
$("#inputGroupSelect01").change(function (){
alert($(this).val());
});
Let's assume the value is an ID and the text is a column in my database. I need to fetch this information to submit a PUT request with the ID and the text. Thanks for your help!
Simply add option:selected to your ID in jQuery:
$("#inputGroupSelect01").change(function (){
alert($("#inputGroupSelect01 option:selected").text())
});

SELECT control, how to set that what you want to show

I have some issue with this:
<select name="whatever">
<option value="pizza">pizza</option>
<option value="pasta">pasta</option>
<option value="sandwich">sandwich</option>
<option value="cheese">cheese</option>
</select>
Select control always when i "run" it show me as first in drop-down menu a pizza and that mean a pizza is selected by default. So, how i can change this if i want to post cheese to be a default(current active in drop-down)
This down is just a example how i want to work my select, you see down a placeholder=5 , that mean this number control will by default use a 5 and i want to do the similar thing with my select control.
<input type='number' placeholder=5 min=0 max=10 step=1>
Thank you!
You simply just put the attribute of selected on to the option you want as your default.
You can use a JavaScript function to get the value from your table and assign this value as the selected option of the drop-down. The options for a select drop-down are identified by an index starting at 0.
In the demo below, I've used buttons to call the JS function. Click on the button to change the selected option:
var sel = document.getElementsByName("whatever")[0];
function defaultOption(option) {
sel.options[option].selected = 'selected';
}
<button onClick="defaultOption(0)">pizza</button>
<button onClick="defaultOption(1)">pasta</button>
<button onClick="defaultOption(2)">sandwich</button>
<button onClick="defaultOption(3)">cheese</button>
</br>
</br>
<select name="whatever">
<option value="pizza">pizza</option>
<option value="pasta">pasta</option>
<option value="sandwich">sandwich</option>
<option value="cheese" selected>cheese</option>
</select>

Select text that is not in the options using jquery

I have this html code,
<select name="sup" class='form-control' required >
<option value="1">Value 1</option>
</select>
Is it possible to display text to the dropdown that is not in the options using JQuery?
More explanation,
I have only option that value is 1 with name of "Value 1".
I want to display "Value 2" with value of 2.
But if I will click the dropdown arrow, It will display only one option, the "Value 1".
Is it possible?
Maybe this code for a placeholder could help you, it will show as default value in the select, but will not be selectable, and the user will be forced by html validation to select another option (if you use HTML validation). If you don't use HTML (or other) validation, because the option is disabled, it won't be sent with the form even if it has a value.
The "Other" option will trigger a new input to have a custom response.
//Detects the change of value of the select element.
$("select[name='sup']").change(function() {
//Checks if the current value of the elemnent is "other"
if ($(this).val() == "other") {
//If so, the other input is shown
$("input[name='other-input']").show();
} else {
//else, the input is cleaned and hidden
$("input[name='other-input']").val("");
$("input[name='other-input']").hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="sup" class='form-control' required>
<option value="" selected disabled>Placeholder Text</option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<option value="other">Other</option>
</select>
<input style="display: none; width: 300px;" type="text" name="other-input" placeholder="Type here your custom response">
No. What shows when the drop-down isn't open must be one of the options in the select's options. To do something else, you'd have to start playing with overlaying things on top of the select, with all the cross-browser pain that comes with it.

On change pass the value of select option box in url

I am trying to pass the value of option box in url on onchange event.I have to pass the value of selected option box to some.php file.Here i am tried out but it's not working for me.
Bellow is my sample code
<select id="font" onchange="document.getElementById('font').src='some.php?fontname='+this.value">
<option value="arial" >arial</option>
<option value="stencil" >stencil</option>
</select>
You need to use javascript location.href for redirecting.
<select id="font" onchange="window.location.href='some.php?fontname='+this.value">
<option value="arial" >arial</option>
<option value="stencil" >stencil</option>
</select>

Get the value of select jquery

<select id="my-select">
<option value="1">This is one</option>
<option value="2" selected>This is two</option>
...
</select>
Is there a way to get the text value of the selected option?
$('#my-select').val();
gives me 2, i want to get This is Two instead.
How?
What you want is
Get the selector for finding the selected option in the select box
Use .text() on the selector.
$('#my-select option:selected').text();
See a working demo

Categories