locked
Find occurence in table inside iframe using jquery RRS feed

  • Question

  • User181930479 posted

    I am trying to access a table and find occurence number in  using jquery , but its not working...

    below is my code: any suggestions ???? :(

    <script>

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

                // here we will store counters for each ...

                var occurencies = {};

                // iterate each cell  (if1 is the name of the iframe.)

                $('#if1').contents().find('table tr td').each(function () {

                    // get number in cell

                    var number = $(this).html();

                    alert(number);

                    // if that number was counted already then exit

                    if (number in occurencies) { return; }

                    // get occurence count for that number

                    var count = $('table#tab td:contains(' + number + ')').length;

                    // and store it

                    occurencies[number] = count;

                });

                // for each found number

                for (var number in occurencies) {

                    // get stored count

                    var count = occurencies[number];

                    // print into #results div

                    $('#results').append('Occurence of ' + number + ': ' + count + '<br>');

                }

            });

                   

        </script>

    Wednesday, December 20, 2017 7:12 AM

All replies

  • User1882717255 posted

    If you trying to detect number of occurrances inside iframe you cannot just simply use:
    var count = $('table#tab td:contains(' + number + ')').length;

    First you have to go inside iframe ( $("#if1").contents()), the same as you doing at the beggining of code:
    $("#if1").contents().find('table#tab td:contains(' + number + ')').length;

    And it could be good approach to save iframe contents to local variable before if you gonna use it more than onece, like:
    var $iframeContents = $("#if1").contents();
    var count = $iframeContents.
    find('table#tab td:contains(' + number + ')').length;

    Wednesday, December 20, 2017 10:08 AM
  • User-1838255255 posted

    Hi NAF,

    You say it doesn't working, could you tell us what code not work? Please check if meet exception message in the console panel, also you could check this tutorial about how to debug JS code:

    https://www.w3schools.com/js/js_debugging.asp 

    if you can not found the error or can not make it work, please share your complete code for us to test! 

    Best Regards,

    Eric Du 

    Monday, December 25, 2017 6:35 AM