jQuery Ajax – tips

jQuery Ajax is async by nature. We use to set a flag “async:false” if we need to make Ajax call sync (non-blocking). This feature has been deprecated. Here is a compile list of Ajax using jQuery;

Alternative to “async: false” for successive AJAX calls

Handling Sequential AJAX Calls using jQuery

Multiple Simultaneous Ajax Requests (with one callback) in jQuery

jQuery callback for multiple ajax calls

jQuery.when understanding

jQuery Promises – Taking action .when() multiple ajax calls are complete

jQuery selector change not firing

I like to fire a change event when “#btnBrowse” click event fires. Here is a sample jQuery code;

$("#btnBrowse").click(function () {

$("#xFileUpload").change(function () {
   var filename = $('#xFileUpload')[0].files[0].name;
   filename = filename.replace(/\.[^/.]+$/, "");
   fileDoc.fileName = filename;
   fileDoc.fileContnet = $("#xFileUpload").get(0).files;

It turns out that click event will be registered and fired on first click only. For subsequent clicks, I have to registered a change event and triggered it like this;

$("#btnBrowse").click(function () {

The cons of using this approach. It will work fine for the first click but all subsequent clicks will fire a change event on click event. The reason jQuery has registered two events for the element and execute them simultaneously.

The objective is to grab file from file system. The user should be able to use same file multiple time. The best alternative to achieve this objective is to clear the contents of HTML input type rather tweaking jQuery;

This is HTML file input;

<input type="file" id="xFileUpload" name="xFileUpload" class="form-control" style="display: none" />

This is jQuery code that will clear contents of this file;

$("#btnImportReset").click(function () {

This is your final browse script section;

$("#btnBrowse").click(function () {

Since we are clearing out HTM contents and jQuery is smart enough to figure out change. The change event will be fired this time automatically with a change in HTML file input.




jQuery click event on view load

This is how to fire jQuery event on ASP.NET Core MVC view load;


<div class="container">
    <div class="nav">
        <ul id="menu">
            <li id="link1"><a href="#">Topic One</a></li>
            <li id="link2"><a href="#">Topic Two</a></li>
            <li id="link3"><a href="#">Topic Three</a></li>
    <div class="main">
        <div id="page1" class="content">
            <h1>Page 1</h1>
            <p>First section of content.</p>
        <div id="page2" class="content">
            <h1>Page 2</h1>
            <p>Second section of content</p>
        <div id="page3" class="content">
            <h1>Page 3</h1>
            <p>Third section of content.</p>


<script type="text/javascript">
    $('#menu a').click(function (e) {
        var tmp_div = $(this).parent().index();
        $('.main div').eq(tmp_div).show();

    function hideContentDivs() {
        $('.main div').each(function () {

    //fire this event on view load
    $(function () {
        $('#menu li a:first').click();


This function does the auto click on view load;

$(function () {
    $('#menu li a:first').click();




Load Bootstrap tabs dynamically

Need Bootstrap tabs, but with dynamic content? Use this JS to fetch the content before showing the tab. Works for in-content links as well.

//the reason for the odd-looking selector is to listen for the click event
// on links that don't even exist yet - i.e. are loaded from the server.
$('#tabs').on('click','.tablink,#prodTabs a',function (e) {
    var url = $(this).attr("data-url");

    if (typeof url !== "undefined") {
        var pane = $(this), href = this.hash;

        // ajax load from data-url
    } else {

Read more here