get selector value after selection? - javascript

I'm curious as to if it is possible to get this selector value after the user selects a value?
<select>
<option>test1</option>
<option>test2</option>
</select>
//Idea of what i'm trying to accomplish
select.addEventListener('select',()=>{
console.log(select.value);
})
I'm trying to use this value data to determine which item to load and view before you submit the form.

First of all, you have to locate your select element in the script. To do this, you need to provide it with a specific id.
<select id="test_selector">
<option>test1</option>
<option>test2</option>
</select>
Then you can use this id to access that selector in the JS. In order to get the updated value, chain it with the change event listener. Its callback receives the event parameter which contains the info you need.
document.getElementById('test_selector').addEventListener('change', event => {
console.log(event.target.value);
});

Another option is to do it on jquery if you feel more comfy with it
Give your select an id
<select id="id_selector">
<option>test1</option>
<option>test2</option>
</select>
Use a jquery .change function to get the value of the selected option
$('#id_selector').change(function(){
console.log($('#id_selector').val());
//or console.log($(this).val()); to make the code shorter
});

Try this
select.addEventListener('select',(e)=>{
console.log(e.target.value);
})

Related

Set the first option select2

I want to set the first position to select2 dropdown by default, I have tried this but It doens´t work:
$('#mylist').val($('#mylist option:first-child').val()).trigger('change');
Other way that I tried;
$('#mylist').val(1);
But the problem is I don´t know what value is, because it is depend from a query and It will not always be the same value.
I did not set the dropdown values ​​from the HTML, but it is an input hidden and the values ​​are loaded in a query
I hope that anyone can help me
Regards!
If you using Select2 4.x just trigger change.select2
$('#mylist').val(1).trigger('change.select2');
please try with this:
$('#yourSelect2').val($('#yourSelect2 option:eq(1)').val()).trigger('change');
the value from eq() depends of the position that you want to select
This works for me:
$('#mylist option:eq(0)').prop('selected',true);
Please do this
$('select#mylist').val(1).select2();
It's better to use attribute specifiers and set that element's selected prop to true like so:
$('option[value="op2"]').prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="list">
<option value="op1">Option 1</option>
<option value="op2">Option 2</option>
<option value="op3">Option 3</option>
<option value="op4">Option 4</option>
</select>
Then change op2 to whatever the value attribute of the desired default option is.
this work for me :
$("#mylist").val($("#mylist option:first").val()).trigger('change');
juste remove -child
As you may know Select2 need data in particular format only [id="id_value",text="data_linked_to_id"]
Lets consider you want to select first item ,you should have its Id which is used by select2 to list all items.(let here first id be 1)
$('#selectElementId').val("1");
$('#selectElementId').trigger('change');
If want to set multiple items selected then pass array of ids.
ids=["1","3","4"]
$("#selectElementId").val(ids);
$("#selectElementId").trigger('change');
**OR**
$("#selectElementId").val(["1","3","4"]);
$("#selectElementId").trigger('change');
Step 1:
Select first option of your select(s) - works not only for select2
$('#myForm').find('select').find('option:eq(0)').attr('selected','selected');
Step 2:
Trigger select2 change
$('#myForm select').trigger('change.select2');

How can I determine what option is selected on a dropdown? [duplicate]

This question already has answers here:
Get selected value of a dropdown's item using jQuery
(31 answers)
Closed 7 years ago.
I have a dropdown menu of values:
<select class="notifications-dropdown">
<option value="val1">Val4</option>
<option value="val2">Val3</option>
<option value="val3">Val2</option>
<option value="val4">Val1</option>
</select>
I want to make it so when a user selects one of the options from the dropdown, that option has a 'selected' attribute toggled on. For example, if a user clicks the dropdown and selects Val3 then the DOM should change as follows:
<select class="notifications-dropdown">
<option value="val1">Val4</option>
<option value="val2" selected>Val3</option>
<option value="val3">Val2</option>
<option value="val4">Val1</option>
</select>
I am not sure what jQuery event to listen for to know when a user selects an option though. I tried listening for a change event on the .notifications-dropdown but the generated event object does not give me any indication about which option was selected. How can I determine what option is selected on a dropdown?
Just use .val() on your select list.
$('.notifications-dropdown').change(function() {
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="notifications-dropdown">
<option value="val1">Val4</option>
<option value="val2">Val3</option>
<option value="val3">Val2</option>
<option value="val4">Val1</option>
</select>
this when used inside of a jQuery callback is the element which the event occurred on.
You seem to be on the right track with the change event. But that is the thing, the change event just tells you that the field/option selected has changed, it, and any other event listener, will not provide a value.
To get the value, you will need to reference jQuery like so:
$('.notifications-dropdown option:selected').text();
or to get the value of the option selected, you can reference it like so:
$('.notifications-dropdown option:selected').val();
You were on the right track:
// on Dom-ready
$(function() {
// bind the 'change' event
$('.notifications-dropdown').change(function() {
// $(this).val() will have the selected value
alert($(this).val());
}).change(); // fire it once on page-load if you need to do anything based on the current value
});
The user's action of selecting a new option will modify the DOM element's selected attribute -- you don't need to do that programmatically.
Use below code on a jquery change event. Assume, select tag has id="selectId"
To Read Select Option Value
$('#selectId').val();
To Set Select Option Value
$('#selectId').val('newValue');
To Read Selected Text
$('#selectId>option:selected').text();
Use .val() method
Get the current value of the first element in the set of matched elements or set the value of every matched element.
The .val() method is primarily used to get the values of form elements such as input, select and textarea.
$('.notifications-dropdown').change(function() {
alert($(this).val());
});

Javascript to select option in dropdown list with VALUE

JSFiddle Demo of My Code
I need to change the selected OPTION in a dropdown & Also Trigger the onChange of the dropdown.
$('#type').val('option 1');
$('#type').val('option1');
I tried the above to select
<option value="option1">option 1</option>
However no luck so far!
Use the following:
$('#type').val('option1').change();
You should pass the value of the value attribute to the .val() function.
You can trigger the change event by calling .change() (with no arguments) on the jQuery object that represents the <select> element.
Note: Calling .change() with no arguments is a shortcut for calling .trigger('change').
jsfiddle
you can use your code like this. #openvz is not present in your html code that's why its not working.and you need to use option1 instead option 1
$('#change').click(function(){
$('#type').val('option1');
});
js fiddle
You need to cancel the click and use the correct ID
$('#change').on("click",function(e){
e.preventDefault();// cancel
$('#type').val('option1').change(); // must be the value, not the text
});
Also you cannot submit a select. If you want, you can submit the form if it does not have a field with name="submit"
You will need to tell us what you want to have happen when you change the select too. for example change the select and have it submit the form it is in
$(function() {
$('#change').on("click",function(e){
e.preventDefault();// cancel
$('#type').val('option1').change(); // assuming you want to trigger the change
});
$('#type').on('change',function() {
$(this).closest("form").submit();
});
});
try this Demo
<option value="option 1">option 1</option>
$('#change').click(function(){
$('#type').val('option 1');
});

How to get the selected value of some random dropdown in Javascript/jQuery

<select ng-model="myModel" id="searchAsset" class="search">
<option ng-repeat="asset in assettype" ng-click="assetclick()" value="{{asset}}"></option>
</select>
<select class="search" id="searchLevel">
<option class="chooseLevel" ng-repeat="level in levellist" value="{{level}}"></option>
</select>
While performing some logic on second dropdown, I want to fetch the selected value of the first dropdown or vice-versa. How can I do this?
I tried using $("#searchLevel").value and $("#searchLevel option:selected").text()
The direct answer to this question is to use the .val() method for jQuery like so:
var selectedVal = $("#searchLevel").val();
However, the slightly less direct, but true answer is that you should not be doing anything like this in an angular app - changes in the dom should only be affecting your view model.
When your using angular, jquery is really not required.
As per your code, The first select menu value will be stored in the ng-model attribute value i.e. myModel.
In your second select menu, specific the ng-model as well. You can just fetch the value of the drop down menu by calling ng-model name.
<select class="search" id="searchLevel" ng-model="secondSelect">
<option class="chooseLevel" ng-repeat="level in levellist" value="{{level}}"></option>
</select>
For example,
If you want to access the value inside your controller on change event, then
$scope.changeEventhandlerForFirstSelect = function() {
// $scope.myModel will contain the value
}
Similarly, for second select menu $scope.secondSelect will give that value.
try
$("#searchLevel").val();
with: $("#searchLevel option:selected").text() you get the text not the value.

Variable's value transfer from Jsp to Javascript

I have a dropdown of roles in JSP file. When a role is selected from the dropdown I want to get the selected role ID. When a role is selected then an onchange event occured and a function 'callAjax(roleId) is triggered. Here the roleId is jsp variable. I am trying to pass it through a javascript. But I can not able to do this. Is it really possible? Can any one help me to doing this.
Thanks in advance.
<select name="role" onchange="callAjax(roleId)">
<c:forEach items="${pro.roles}" var="rol" varStatus="status">
<option value="${rol.id}">
<c:out value="${rol.name}"/>
</option>
</c:forEach>
</select>
I guess you want to use the value which contains the id, so change to this:
onchange="callAjax(this.value)"
Remove the onchange="callAjax(roleId)" attribute from your select, and bind a change handler to the element with jQuery. The value returned by val will be the value from the selected option, which corresponds to the rol.id assigned to that option.
$(function() {
$("select[name='role']").change(function() {
callAjax($(this).val());
});
});
try this :
onchange="callAjax('<%=roleId%>')"

Categories

Resources