0

I am trying to get the value of a checkbox to store in my database, but my code crashes right after running the serialized array.

Here is the javascript:

$(function () {

$('.form-signin').on('submit', function (e) {

    e.preventDefault();

    var data = $(this).serializeArray(),
        pname = data[0].value,
        score = data[1].value,
        cheatm = data[2].value;

    var GameScore = Parse.Object.extend("GameScore");
    var gs = new GameScore();

    gs.set("score", parseInt(score));
    gs.set("playerName", pname);
    gs.set("cheatMode", cheatm === 'true');
    gs.set("user", Parse.User.current());
.
.
.

It crashes after cheatm = data[2].value;

Here is the HTML:

<form class="form-signin" role="form">
                <h2 class="form-signin-heading" id="login-greeting">Enter Game Score</h2>
                <input type="text" name="Player Name" class="form-control" placeholder="Player Name" required="" autofocus="">
                <input type="number" name="Score" class="form-control" placeholder="Score" required="">
                <input type="checkbox" value = 'true'> Cheat Mode<br>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form>
3
  • Why not give the checkbox an id, and then set to the value to $("myCheckbox").is(":checked");? Commented Nov 5, 2015 at 16:58
  • That was a good answer. If you post it below I'll accept it - if you want. Commented Nov 6, 2015 at 0:14
  • I converted it to an answer. Checkboxes tend to get weird sometimes. Commented Nov 6, 2015 at 0:22

1 Answer 1

1

You could give the check-box an id and call it in JavaScript like:

HTML:

<input type="checkbox" id="myCheckbox"/>

jQuery:

var isMyCheckboxChecked = $("#myCheckbox").is(":checked");
Sign up to request clarification or add additional context in comments.

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.