0

I have a an HTML page with an input area and a submit button.

I am using jQuery to alert the user of what their input is. (Really I am trying to store that in a variable but that will be trivial once I get this working).

Can someone help me understand why it is not working, as well as a solution? Thanks.

The HTML form is as follows:

<html>
    <head>
    </head>
    <body>
        <form>
            <input type="text" class="grid_size">
            <button type="submit">Submit Me!</button>
        </form>
        <!-- Add jQuery -->
        <script src="js/jquery.js"></script>
        <script src="js/custom.js"></script>
    </body>
</html>

The custom jQuery I am using is this:

$(document).ready(function() {
    $("form").submit(function() {
    alert( grabUserInput )});
});

function grabUserInput() {
    return $("form").find(".grid-size").val();
}
1

3 Answers 3

2

You named your class grid_size, and search for a class grid-size in javascript code. This code should fix a typo:

$(document).ready(function() {
    $("form").submit(function() {
        alert( grabUserInput() );
    });
});

function grabUserInput() {
    return $("form").find(".grid_size").val();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I also forgot the parenthesis at the end of the grabUserFunction().
1

Try this -- grabUserInput is a function, not a variable -- so you have to include () to invoke the function:

$(document).ready(function() {
    $("form").submit(function() {
        alert( grabUserInput() );
    });
});

And your markup has to match your selector:

 <input type="text" class="grid-size">

BONUS:

In case all you want is to alert the value, and you do not want to submit the form use the following:

$(document).ready(function() {
    $("form").submit(function(e) {
        e.preventDefault()
        alert( grabUserInput() );
    });
});

Comments

0

why not try to put some id

<input id="input_grid_size" type="text" class="grid_size">
<button id="button_submit" type="submit">Submit Me!</button>

and jq

$(document).ready(function() {
    $("#button_submit").click(function() {
    alert($('#input_grid_size').val()); }); });

it seem your code also forgot the ";" and "()" in the alert

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.