Ad

Track Which Dynamic Anchor Tag Was Submitted In Javascript

- 1 answer

I don't have much experience with JavaScript.
If User has already pressed on this dynamic anchor tag, I want the that it alerts him with a different type of confirm message.

<table class="table table-bordered">
    <thead>
        <tr>
            <th scope="col">Dataset</th>
        </tr>
    </thead>
    <tbody>

        @foreach ($dumpDb as $key => $value)
        <tr>
            <td scope="row">{{ $value->dataset }}
                <span>
                    <a class="downloadLink" target="_blank" rel="nofollow noreferrer" href="{{Route('dump.downloadFile', ['id' => $value->dataset ])}}" onclick="return ConfirmDownload()"> Download </a>
                </span>
            </td>
        </tr>
        @endforeach

    </tbody>
</table>
<script type="text/javascript">
// dalifyDownloads - how many times a user can download file in 24hours. By default 5 files per day.
    function ConfirmDownload() {
        var dailyDownloads = {{ Auth::user()->dailyDownloads}};
        if (User has already pressed on this button) {
          var x = confirm("Are you sure you want to download this file?");
          if (x)
              return true;
          else
              return false;
        } else {
          // If User has not pressed on this button
            var x = confirm("Are you sure you want to download this file? Your daily download limit is " + dailyDownloads);
            if (x)
                return true;
            else
                return false;
        }
    }
</script>
Ad

Answer

You can keep track using a variable outside the function:

var pressed = false;

function ConfirmDownload() {
    var dailyDownloads = {{ Auth::user()->dailyDownloads}};
    if (!pressed) {      
      var x = confirm("Are you sure you want to download this file?");
      if (x){
          pressed = true;
          return true;
      }
      else
          return false;
    } else {
      // If User has not pressed on this button
        var x = confirm("Are you sure you want to download this file? Your daily download limit is " + dailyDownloads);
        if (x)
            return true;
        else
            return false;
    }
}
Ad
source: stackoverflow.com
Ad