简体   繁体   中英

How to get array values Into another function?

I have a jQuery function where I can get the table rows values and store into array and return this array. I want to use this array return values into another function but I got error like underdefined 0,underdefined length. I try multiple options like $.each method, for loop and last one called direct array but can't get result.

function rowData(btn_id){
    $(document).ready(function () {
    $(document).on('click', btn_id, function () {

        var getdata = $(this).closest('tr').children("td").map(function () {
            return $(this).text();
        }).get();

        console.log(getdata);
        return getdata;

        });
    });
}
var all = rowData(".edit_minor_cate");
alert(all[0]);

Please help me to fix it.

Try this HTML:

<div>
  <table>
    <tr>
      <td class="data"> Hello, I am data 1 </td>
      <td class="data"> Hello, I am data 2 </td>

    </tr>
  </table>

  <button id="button_id">click me</button>
</div>

And JavaScript:

$(document).ready(function () {
    $(document).on('click', "#button_id", function () {
        var getdata = $('.data').map(function(){
               return $.trim($(this).text());
            }).get();
        console.log(getdata[0]);
        alert(getdata[1]);
    });
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM