1

I am using an onclick() function to add an item to cart.

<button class='btn pull-right' id = 'cartBtn' onclick = 'addToCart(<?php echo $id;?>)'><span class='glyphicon glyphicon-shopping-cart'></span></button>

This is the js:

$(document).ready(function(){
    var cart = [];
    var id = $("#itemId").text();
    var stockAvailable = $("itemStock").text();
    var inCart = false;


    function addToCart(item){
        id = item;
            if (inCart == true){
                console.log("item already in cart");
            }
            else{
                cart.push(id);

            }

    }

However, I get the following error in the console upon clicking the button:

ReferenceError: addToCart is not defined

I have written the js code on a separate file and inluded it in the head section. What could be the problem here

5
  • 2
    Just write function outside "$(document).ready" because function doesn't need to wait until jquery gets initialised. Commented Jul 9, 2019 at 4:29
  • this is exactly your script is? Commented Jul 9, 2019 at 4:32
  • Yeah, thats the script Commented Jul 9, 2019 at 4:34
  • aren't you getting more syntax error or something? Commented Jul 9, 2019 at 4:36
  • @RAVIPATEL no. The only problem is that the onclick() function cannot seem to find the addToCart() function. Commented Jul 9, 2019 at 4:38

4 Answers 4

2

You cannot use addToCart without defining it. You can define it in the place where you want to use this function.

$(document).ready(function() {
  function addToCart(item) {
    console.log('added');
  }
});
addToCart('item');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Right way

function addToCart(item) {
  console.log('added');
}
$(document).ready(function() {

});
addToCart('item');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

1 Comment

I have defined it in an external script.
1

from the official jquery docs. https://learn.jquery.com/using-jquery-core/document-ready/

TLDR: document.ready runs only once so define your function outside of it.

Comments

1

Here i am showing you very basic example code based on your question

var id, cart = [], stockAvailable, inCart = false;
$(document).ready(function(){
  id = $("#itemId").text();
  stockAvailable = $("itemStock").text();
});

function addToCart(item){
  id = item;
  if (inCart == true){
      console.log("item already in cart");
  }
  else{
      cart.push(id);
      inCart = true;
      console.log("item added in cart");
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='btn pull-right' id = 'cartBtn' onclick = 'addToCart(1)'><span class='glyphicon glyphicon-shopping-cart'></span> Add to Cart</button>

Comments

0

Anyways you are using Jquery, so can you please check with jquery event handler.

Please find the answer using jquery click event.

$("#cartBtn").on("click", function(item){
       id = item;
            if (inCart == true){
                console.log("item already in cart");
            }
            else{
                cart.push(id);

            }
        }
   })

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.