Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Uncaught ReferenceError: buyNft is not defined at HTMLAnchorElement.onclick

Am I missing to add some script or what? I am getting error from title. How can I solve this, if you can help me, I would be grateful. I tried adding different scripts, but I amgetting the same error.

 <a onclick="buyNft({{$nft->id}})">{{trans('nft.buy')}}</a>
    
        @push('scripts')
            <script>
        
                function buyNft(nftid) {
                    swal({
                        title: "Are You Sure?",
                        text: "Do you want to buy this nft?",
                        type: 'warning',
                        showCancelButton: true,
                        showConfirmButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
        
        
                    }).then((result) => {
                        if (result) {
                            axios.post("/buythisnft/" + nftid).then(response => {
                                window.location.reload();
                            });
        
                        }
                    });
        
                }
            </script>
        @endpush
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your buyNft-function needs to be declared BEFORE you echo out any element with "onclick=buyNft()":

@push('scripts')
  <script>
      function buyNft(nftid) {
          // ...
      }
  </script>
@endpush
 
 <a onclick="buyNft({{$nft->id}})">{{trans('nft.buy')}}</a>

UPDATE: This is an example using a script at the bottom of the page which creates an eventlistener for click-events:

document.addEventListener("click",e => {
  if(e.target.closest(".buyNft")){
    e.preventDefault();

    alert("You want to buy an NFT.")
    
    //... Replace with your own functions ...
  }
})
<a class="buyNft" href="#">Buy that NFT</a>
<br /><br />
<a href="#">Some other link on this page</a>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!