How to trigger jQuery change event on dropdown with same value
This should work. Its a combination of flags and click events. It will always get triggered when you click on an option.
<select class="businessTypePullDown">
<option value="" disabled selected>Business Type</option>
<option value="1">General</option>
<option value="2">Lawyer</option>
<option value="3">Software Development</option>
<option value="4">Auto-repair</option>
</select>
(function () {
var filterPullDown = function() {
alert('clicked');
}
var flag = false;
$(".businessTypePullDown").click(function () {
if (flag) {
filterPullDown()
}
flag = !flag;
});
$(".businessTypePullDown").focusout(function () {
flag = false;
});
}());
trigger change() even on selecting the same value for select field
Do it on click
$('#hall').on('click',function(){
if(this.selectedIndex == -1) {
alert('select one answer');
}
else{
// do whatever you want!
}
});
Trigger the event when selected the same value in dropdown?
Simple JS
---------
<html>
<head>
<script>
var prevIndex = "";
function onSelect()
{
var currIndex = document.getElementById("ddList").selectedIndex;
if( currIndex > 0 )
{
if( prevIndex != currIndex )
{
alert("Selected Index = " + currIndex);
prevIndex = currIndex;
}
else
{
prevIndex = "";
}
}
}
</script>
</head>
<body>
<select id="ddList" onClick="onSelect()">
<option value="0">Select Me</option>
<option value="1">List1</option>
<option value="2">List2</option>
<option value="3">List3</option>
</select>
</body>
</html>
Trigger change() event when setting <select>'s value with val() function
I had a very similar issue and I'm not quite sure what you're having a problem with, as your suggested code worked great for me. It immediately (a requirement of yours) triggers the following change code.
$('#selectField').change(function(){
if($('#selectField').val() == 'N'){
$('#secondaryInput').hide();
} else {
$('#secondaryInput').show();
}
});
Then I take the value from the database (this is used on a form for both new input and editing existing records), set it as the selected value, and add the piece I was missing to trigger the above code, ".change()".
$('#selectField').val(valueFromDatabase).change();
So that if the existing value from the database is 'N', it immediately hides the secondary input field in my form.
Related Topics
How to Sort a Map by Value in JavaScript
How to Concatenate a Variable for a Hyperlink
Javascript Return True or Return False When and How to Use It
How to Check If a Cookie Exists
Find Minimum Numbers of Currency for Required Amount
How to Concatenate String and Numbers to Make a Json Object
How to Get Only First Character Match With Regex
Convert Time Stamp to Date Inside Angular Expression Like {{New Date(Timestamp)}}
How to Display Loading Message When an Iframe Is Loading
How to Force Update Child Component from Parent Component in Reactjs
How to Select All Textframes in Document, and Retrieve the Text in Adobe Indesign
Html Input Type Number Thousand Separator
How to Import the Following Function in Another File
How to Access Session Variables and Set Them in JavaScript
How Display Selected Item from Bootstrap 4 Drop-Down Item Menu