Set the first option select2 - javascript

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');

Related

How do I show the selected value of select2?

I am trying to set the value dynamically to my combobox on which I have applied select2 jQuery class. After applying code it sets the value to that combobox; but it still says "Select one".
Can anyone help me to show the selected value? Please.
HTML:
<select id="cmboDemo">
<option value="">Select one</option>
<option value="Bob">Bob</option>
<option value="Scott">Scott</option>
</select>
Javascript:
$("#cmboDemo").select2();
//....
$("#cmboDemo").val("Scott");
...//
In result scott is selected but combobox shows "Select one".
Remove $("#cmboDemo").select2();
and use the second one only:
$("#cmboDemo").val("Scott");
By using select2.js some additional layers of css are overlapped actual combo-box. By using all above code the value is set to select box; but because of that upper style layer it shows first value. i.e Select one.
So by changing that upper layer's text will fixed the problem.
This is the final outcome:
$("#select2-cmboDemo-container").text("Scott");
Thanks.

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.

Angular blank option selected

I'm banging my head against the wall on this one.
I have an array of objects that will be used to populate a select drop down:
CardCount = [{"ClientId": "0010", "Description": "0010 (206 Members)"}, {"ClientId": "0051", "Description": "0051 (1 Member)"}, ........]
When I attempt to use ng-options, the value of the option is set to the index, not to the ClientId as desired. To get the value in each option to be the ClientId, I have to use a ng-repeat in the options. Here is my html:
<select ng-model="CurrentClient">
<option ng-repeat="item in CardCount" value="{{item.ClientId}}">{{item.Description}}</option>
</select>
Initially, all is well, the select and options are generated correctly, and the first option is correct. Now, when a certain button is clicked somewhere else on the page, it becomes necessary to recreate this select and options with a smaller array of similar objects. However, doing so creates a blank option with a value of "? string:0010 ?". This is the option that is selected. Again, I cannot use ng-options to correct this problem because doing so doesn't set the value attribute in the option tags correctly. So, I added this to the option tag:
<option ng-repeat="item in CardCount" value="{{item.ClientId}}" ng-selected="CurrentClient == item.ClientId">{{item.Description}}</option>
Now, that does mark the correct option as selected. However, the drop down still shows the blank option. Here's the rendered html:
<select ng-model="CurrentClient">
<option value="? string:0010 ?"></option>
<option value="0010" selected="selected">0010 (206 Members)</option>
</select>
As you can see, it sets the correct option to selected. However, it sets it to selected="selected", and not just selected. When I inspect element and change selected="selected" to selected (remove the equals and everything after it), the drop down then correctly displays the correctly selected option.
Again, initially the select and options work great. The problem seems to happen only after the array that the select is created with is changed. How can I get this select and options working correctly after I change the array, and not show that first blank option?
Thanks!
Changed you option element to set value by default.
<option ng-repeat="item in CardCount track by item.ClientId"
value="{{item.ClientId}}">{{item.Description}}</option>
Hope this could help you. Thanks.
ng-options is definitely the way to go:
<select ng-model="selected.ClientId" ng-options="it.ClientId as it.Description for it in clientList">
<option value="">-</option>
</select>

How to get the selected option value from select inside div

I have the following html code
<div id="BrokersList4ServiceEndWrapper">
<select id="BrokersList4ServiceEnd">
<option value="1">s1</option>
<option value="2">s2</option>
</select>
</div>
I tried to
set the selected value to 1.
get the value of the second option.
get the selected value
in jquery but didnt get it right and please adive if there is any method to know the selection syntax easily
set the selected value to 1.
$('#BrokersList4ServiceEnd').val(1);
get the value of the second option.
$('#BrokersList4ServiceEnd option:eq(1)').val()
get the selected value
$('#BrokersList4ServiceEnd').val()
DEMO

JS/Jquery: how to check whether dropdown has selected values?

I've googled and tried a number of ways to do this but none work for me so far. What I am looking for is quite simple: I want to be able to tell whether a dropdown has a selected value or not. The problem is that selectedIndex, :selected, val(), etc. do return results for the following case:
<select>
<option value="123">123</option>
<option value="234">234</option>
</select>
Obviously the browser will display this dropdown with the 123 option being selected but it will be selected only because there are no other options, in reality this dropdown doesn't have a selected value because there is no "selected" property. So basically I am trying to find how to tell apart the above dropdown from this one
<select>
<option selected value="123">123</option>
<option value="234">234</option>
</select>
var hasValue = ($('select > [selected]').length > 0);
Alternatively,
var hasValue = $('select').has('[selected]');
Quick solution:
<select>
<option selected></option>
<option value="123">123</option>
<option value="234">234</option>
</select>
Then see if you have a .val()
The approved answer doesn't seem to work for me.
Here is how I do it to check if all select options are selected:
if($('select option:selected').length > 0) {
/* Do your stuff here */
}
As far as I can tell, there is no functional distinction between your two examples. Essentially, the browser automatically selects the first option.
See, for example, the result of
$('option:selected')
on your first example.
If you really want to prevent this happening, you have two options. The first is to introduce a new, empty element into the select, per Jason's answer. The other option is to deselect the automatically selected value:
$(document).load(function(){
$('option:selected').attr('selected', false);
});
This clears the selection. Any result of $('select').val() that isn't an empty string will therefore be a change by the user.

Categories

Resources