I want to write one JavaScript code line in onClick function and also to call another function in the same onClick. I have tried as follow:
<select onchange="updateSeatsDinnerCategory(3993); javascript: return function(){$(this).prop('disabled', true); return true;};" class="width-80" id="rowDinnerCategorySelect3993">
<option value="-1">--select--</option>
<option value="1">Family</option>
</select>
But it's not working.
function(){ ... }
is a function definition. If you want the code inside to be executated instantly, you need to also call it:
(function(){ ... })()
Your code then becomes:
<select onchange="updateSeatsDinnerCategory(3993); return function(){$(this).prop('disabled', true); return true;}();" class="width-80" id="rowDinnerCategorySelect3993">
<option value="-1">--select--</option>
<option value="1">Family</option>
</select>
Looking at your code better, (assuming it's the actual code you want to use), you don't need the IIFE, nor the return value (which is always true):
<select onchange="updateSeatsDinnerCategory(3993); $(this).prop('disabled', true);" class="width-80" id="rowDinnerCategorySelect3993">
<option value="-1">--select--</option>
<option value="1">Family</option>
</select>
Use this:
<select onchange="updateSeatsDinnerCategory(3993);$(this).prop('disabled', true); return true;" class="width-80" id="rowDinnerCategorySelect3993">
<option value="-1">--select--</option>
<option value="1">Family</option>
You can do like this:
<select onchange="(function() {updateSeatsDinnerCategory(3993); $(this).prop('disabled', true); return false;})()" class="width-80" id="rowDinnerCategorySelect3993">
<option value="-1">--select--</option>
<option value="1">Family</option>
</select>
Pass an optional callback to the updateSeatsDinnerCategory function
function updateSeatsDinnerCategory(seats, callback) {
// do whatever you wanted to do
if (typeof callback !== 'undefined') {
callback.call(this);
}
}
This way you can call the function if passed, and skip where you don't want.
// this will call the "standard version"
updateSeatsDinnerCategory(3993);
// this will execute the callback function as well
updateSeatsDinnerCategory(3993, function () {alert('lol'));
The callback does not have to be an anonymous function of course, just any function within scope will work.
EDIT
There are better ways of checking for undefined, but this is the easiest one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.