0
<input type="checkbox" name="cars" value="ferrari" id="F40" onclick="addToBasket(this);">F40

function addToBasket() {
    var bkval = $(this).val();
    console.log(bkval);
}

Why is jQuery not getting the value of the checkbox? I can't seem to understand why, it gets the value fine if I use a standard onClick but it doesn't seem to like calling the function...

1
  • (Unrelated, but why are you passing this if you're not using it?) Commented May 5, 2016 at 16:35

2 Answers 2

1

Your issue is with the this current element, as you are already passing the current element to the function, receive it and use it like

function addToBasket(elem) {
  var bkval = $(elem).val();
  console.log(bkval);
}
<input type="checkbox" name="cars" value="ferrari" id="F40" onclick="addToBasket(this);">F40

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

Comments

1

You need to get value of this which is element that you use onclick

function addToBasket(e) {
  var bkval = e.value;
  console.log(bkval);
}
<input type="checkbox" name="cars" value="ferrari" id="F40" onclick="addToBasket(this);">F40

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.