I have 4 dropdown in my page .I want to know which dropdown is selected or changed using index,.But every time i am getting 0 index.here is my code
https://jsbin.com/fapahosenu/1/edit?html,js,output
$(function(){
function dropdownChangeHandler(){
console.log($(this).index())
}
$('[data-dropdown="dropdowns_js"]').on('change',dropdownChangeHandler);
})
this is not I am wanted ..I want if I change first dropdown it gives me 0.
I change second dropdown it gives me 1.I change third dropdown it gives me 2
The issue is because you're getting the index() from the select yet they are not siblings, hence they all return 0.
To fix this pass a selector to index() which is the group of elements you wish to find the index in, like this:
$('[data-dropdown="dropdowns_js"]').on('change', function() {
var index = $(this).index('[data-dropdown="dropdowns_js"]');
console.log(index);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="bank">
<option selected="true" disabled="true" value="0">Select Bank</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="state">
<option selected="true" disabled="true" value="0">Select State</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</span>
<span>
<select class="dropdowns_js" data-dropdown="dropdowns_js" data-key="district">
<option selected="true" disabled="true" value="0">Select District</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</span>
Add the event onclick and pass the value with function dropdownChangeHandler(1) like this
Try with below function it will give you the selected drop-down index. Please mark as answer, if your problem resolve with my solution
function dropdownChangeHandler(){
$('.dropdowns_js').each(function(a,b){
if($(this).is(":focus"))
{
console.log(a);
}
});
}
If you select state drop-down, It will give you 1 as selected index
Related
Here is a dropdown menu of a website
<select name="ctl00$ddlWersjeJezykowe" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ddlWersjeJezykowe\',\'\')', 0)" id="ddlWersjeJezykowe" style="width:100px;margin-right: 10px;">
<option selected="selected" value="1">Polska</option>
<option value="2">English</option>
<option value="17">Русская</option>
<option value="19">Українська</option>
<option value="20">Deutsch</option>
<option value="21">Français</option>
<option value="22">Español</option>
<option value="24">Português</option>
<option value="25">Türk</option>
</select>
I want to change the language to English through browser console. I tried this on my console
document.getElementById("ddlWersjeJezykowe").value="2";
It only selects English but doesn't change the language. How can i change the language to English through my browser console?
Changing the value programmatically doesn't fire the onchange event, so you have to call setTimeout('__doPostBack(\'ctl00$ddlWersjeJezykowe\',\'\')', 0) too.
Alternatively, you can call document.getElementById("ddlWersjeJezykowe").onchange()
This is will get the selected value from select dropdown
<select id="language" style="width:100px;margin-right: 10px;">
<option selected="selected" value="1">Polska</option>
<option value="2">English</option>
<option value="17">Русская</option>
<option value="19">Українська</option>
<option value="20">Deutsch</option>
<option value="21">Français</option>
<option value="22">Español</option>
<option value="24">Português</option>
<option value="25">Türk</option>
</select>
function GetValue() {
var e = document.getElementById("language");
var result = e.options[e.selectedIndex].value;
document.getElementById("result").innerHTML = result;
}
<button type="button" onclick="GetValue()">Get Selected Value</button>
<div id="result"></div>
I have JSP page and drop down list programatically populated.
Now, under some circumstances that dropdown should be disabled, but user should be still able to see all of it's element, but can't actually select it, something like this: https://jsfiddle.net/nenadbulatovic/6g4pmmpv/1/
<select>
<option value="Select...">Select...</option>
<option disabled value="volvo">Volvo</option>
<option disabled value="saab">Saab</option>
<option disabled value="mercedes">Mercedes</option>
<option disabled value="audi">Audi</option>
</select>
Obviously as there is condition I can't just put disable next option.
This is actual part of code (drop down) where I need that.
<div class="row-fluid">
<div class="span3">
<label><%=Constants.REQUIRED%> <c:out value="${requestScope.RESOURCES_MAP.SITE_PARENT}"/>:</label>
<select class="selectpicker" data-width="100px" id="siteParentNameSelect" style="width:250px;margin-bottom:0px;">
<option value=''>${requestScope.RESOURCES_MAP[SELECT]}</option>
<c:forEach var="siteParentName" items="${siteParentNameList}">
<option value="${(pageContext.request, siteParentName.id)}" ${siteParentName.id == '0' ? 'selected = "selected" ' : ''}> ${siteParentName.name}</option>
</c:forEach>
</select>
</div>
</div>
I should check if some condition like "enabledFlag" and then if it is true/false enable/disable its elements for selection.
EDIT: I can use only JSP/JSTL, JavaScript, JQuery
You should use Angular ng-disabled directive:
<select>
<option value="Select...">Select...</option>
<option ng-disabled=flag value="volvo">Volvo</option>
<option ng-disabled=flag value="saab">Saab</option>
<option ng-disabled=flag value="mercedes">Mercedes</option>
<option ng-disabled=flag value="audi">Audi</option>
</select>
For more info: https://www.w3schools.com/angular/ng_ng-disabled.asp
EDIT:
Unfortunatelly, I am not quite familiar with JSTL, but can't you do something like this?
<option value="mercedes" <c:if test="${flag}"><c:out value="disabled='disabled'"/></c:if>">
I want to change multiple select buttons's values with another select button.
Practically when someone select an option in the main select button, the other select buttons must change with that value.
EDIT: I added the code, the first is the main select button and after I select somethig there, it should change in the others.
<select class="select_format">
<option value="0.49">9x13</option>
<option value="0.59">10X15</option>
<option value="0.99">13X18</option>
<option value="1.20">15X21</option>
<option value="2.60">20X30</option>
</select>
<select name="format[]" class="format_imgs">
<option value="0.49">9x13</option>
<option value="0.59">10X15</option>
<option value="0.99">13X18</option>
<option value="1.20">15X21</option>
<option value="2.60">20X30</option>
</select>
<select name="format[]" class="format_imgs">
<option value="0.49">9x13</option>
<option value="0.59">10X15</option>
<option value="0.99">13X18</option>
<option value="1.20">15X21</option>
<option value="2.60">20X30</option>
</select>
<select name="format[]" class="format_imgs">
<option value="0.49">9x13</option>
<option value="0.59">10X15</option>
<option value="0.99">13X18</option>
<option value="1.20">15X21</option>
<option value="2.60">20X30</option>
</select>
Thanks.
Since you didn't supply code here's a guess:
$(".myTriggerButton").on("click", function() {
$(".buttonToChangeValue").each(function() {
this.value = "new value";
});
});
$('.select_format').change(function(){
$('.format_imgs').val( $(this).val() );
});
https://jsfiddle.net/7hno0ob8/1/
I need to change the 3rd field automatically depending on first two field. ( it would be best if I change 3rd field and then first 2 fields changes also )
I need a Jquery solution. Anyone can help me please?
<select id="First">
<option val="car">car</option>
<option val="phone">phone</option>
</select>
<select id="Second">
<option val="car">car</option>
<option val="phone">phone</option>
<option val="boll">boll</option>
</select>
<hr>
<select id="ChangeItAutomatically">
<option val="carcar">car car</option>
<option val="carphone">car phone</option>
<option val="carboll">car boll</option>
<option val="phonecar">phone car</option>
<option val="phonephone">phone phone</option>
<option val="phonecarboll">phone boll</option>
</select>
Is this what you're looking for?
$(document).ready(function() {
// on change for the 3rd select
$("#ChangeItAutomatically").on("change", function() {
// get the value and split on space
var value = $(this).val().split(" ");
// select the other two selects based on the values
$("#First").val(value[0]);
$("#Second").val(value[1]);
});
// on change for the first two selects
$("#First, #Second").on("change", function() {
// set the value of the 3rd select based on the combination of values of the first two
$("#ChangeItAutomatically").val($("#First").val() + " " + $("#Second").val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="First">
<option val="car">car</option>
<option val="phone">phone</option>
</select>
<select id="Second">
<option val="car">car</option>
<option val="phone">phone</option>
<option val="boll">boll</option>
</select>
<hr>
<select id="ChangeItAutomatically">
<option val="car car">car car</option>
<option val="car phone">car phone</option>
<option val="car boll">car boll</option>
<option val="phone car">phone car</option>
<option val="phone phone">phone phone</option>
<option val="phone boll">phone boll</option>
</select>
good hi, i am using Chosen js https://harvesthq.github.io/chosen/
but I need to call a function when you do not find any value in the list
that's my code:
i have this :
<select name="cboTipoProducto" id="cboTipoProducto" class="chzn-select" <option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<script type="text/javascript">$(function(){
$(".chzn-select").chosen({no_results_text: "<a href='myfunction' >"}); });
but not working please help me . thanks