How to Trigger Change of Option in Dropdown When Options Can Have Same Value

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



Leave a reply



Submit