1

i have a code like below:-

$(document).ready(function () {
    $("#tableA").on("click", function () {
         $.ajax({
         type: "POST",
         url: "Testing.ashx",
         success: function (output) {
            output = JSON.parse(output);
            DrawTableA(output);
            }
         })
    });

     $("#tableB").on("click", function () {
         $.ajax({
         type: "POST",
         url: "Testing.ashx",
         success: function (output) {
            output = JSON.parse(output);
            DrawTableB(output);
            }
         })
    });          

}

<table id=tableA>tableA</table>
<table id=tableB>tableB</table>

and im hoping to make it only into one function instead of using two function as the attributes are all the same

0

5 Answers 5

2

Firstly you can join the selectors with a comma to combine the event handlers. The only difference from there is the function which gets called in the success function. Assuming the logic there is the same too you could pass the table as an argument:

$(document).ready(function () {
  $("#tableA, #tableB").on("click", function () {
     let $table = $(this);
     $.ajax({
       type: "POST",
       url: "Testing.ashx",
       success: function (output) {
         let output = JSON.parse(output);
         DrawTable($table, output);
       }
    })
  });
});

If, for whatever reason, you need to execute completely different logic on each table element, then you could store the function name in a data attribute on the table HTML. Then you can define the functions within an object, keyed by the value of that attribute:

let tableFuncs = {
  foo: () => console.log('tableA'),
  bar: () => console.log('tableB')
}

$(document).ready(function() {
  $("#tableA, #tableB").on("click", function() {
    let $table = $(this);
    
    // inside AJAX callback...
    tableFuncs[$table.data('func')]();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tableA" data-func="foo">
  <tr>
    <td>tableA</td>
  </tr>
</table>
<table id="tableB" data-func="bar">
  <tr>
    <td>tableB</td>
  </tr>
</table>

Also, as pointed out by @PeterSH in the comments, it's better practice to use common classes on repeated elements to make the code easier to maintain. With your current pattern, if you add a #tableC in the future you would need to remember to amend the JS as well. If you select by a class you just need to remember to include the class in the HTML only.

Sign up to request clarification or add additional context in comments.

2 Comments

It might be better to have a class selector, if more tables are added in the future. $(".drawTable").on("click", function () {})
Very good point, thanks. I added a note about it in the answer.
0

i think something like this would work

$("#tableB, #tableA").on("click", function () {
    var targetTable = $(this);
         $.ajax({
         type: "POST",
         url: "Testing.ashx",
         success: function (output) {
            output = JSON.parse(output);
            if(targetTable.attr('id') === 'tableA') {
             DrawTableA(output);
            } else {
             DrawTableB(output);
            }
            }
         })
    });         

just use , delimiter for each target element

Edit: use the attribute id on which table being clicked to and then call a function for that table for rendering

4 Comments

DrawTableA() and DrawTableB(), though
@noobHere the function in the ajax is different, its DrawTableA() and DrawTableB()
you can check the attr.id and call the specific function for that.. see edited answer
guys this is my new thread, stackoverflow.com/questions/63170853/…
0
You can use this code for helpful 

$(document).ready(function () {
    $("#tableA, #tableB").on("click", function () {
         $.ajax({
         type: "POST",
         url: "Testing.ashx",
         success: function (output) {
            output = JSON.parse(output);
            DrawTableA(output);
            }
         })
    });

}

<table id=tableA>tableA</table>
<table id=tableB>tableB</table>

Comments

0

Will work for all ids starting with the table:-

 $(document).ready(function () {
$("[id^=table]").on("click", function () {
 let $table = $(this);
     $.ajax({
     type: "POST",
     url: "Testing.ashx",
     success: function (output) {
        output = JSON.parse(output);
        DrawTable($table,output);
        }
     })
});        }

Comments

0

I did it this way and it works.

            $(".tableToggle").on("click", function () {
                var tableType = $(this).attr('data-tableid');
                var fName = "DrawTable" + tableType;
                $('body').append('<div id="loadingDiv"><div class="lds"><div></div><div></div><div></div></div></div>');
                if (($("#tb2" + tableType + " tr").length) > 0) {
                        $("#tb1" + tableType).toggleClass("accordion accordion2");
                        $("#tb2" + tableType).toggle();
                        $("#loadingDiv").remove();
                    
                }
                else {
                    $.ajax({
                        type: "POST",
                        url: "AccInfoTest.ashx",
                        data: { itemType : tableType},
                        success: function (output) {
                            try {
                                output = JSON.parse(output);
                                window[fName](output);
                            }
                            catch (ex) {
                                alert("error");
                                $('#tb2' + tableType).empty();
                            }
                        }
                        , complete: function (data) {

                                $("#tb2" + tableType).show();
                                $("#tb1" + tableType).addClass("accordion2").removeClass("accordion");
                                $("#loadingDiv").remove();
                            
                        }
                    })
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.