Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JavaScript/JQuery tables/forms

I am having some trouble creating a Javascript/Jquery table. Basically, I need to output the answers typed in the form to the page. But I am not 100% sure how to do this.

<!DOCTYPE html>

<html>

<head> <meta charset="utf-8"> <title>Homework - Week 8</title>

<link href="stylesheet.css" rel="stylesheet" type="text/css">

</head>

<body> <div id="container"> <h1> New baby items</h1> <form id="newItemForm"></form> <h2>Gift Registry</h2>

    <label for="item">Item Name</label>
    <input type="text" id="item" name="item" />




    <!--
        <label for="quantity">Quantity Desired</label>
        <input type="number" id="quantity" name="quantity" />
        <label for="location">Location</label>
        <input type="text" id="location" name="location" />
        <label for="price">Price</label>
        <input type="number" id="price" name="price" />

-->

    </form>
    <div id="newItemButton">
        <button id="submit">Submit</button>

        <table id="tableShell">
            <tr>
                <td></td>
            </tr>
        </table>
    </div>






</div>

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>


<script src="script.js"></script>

</body>

</html>

$(document).ready(function () {

$("#submit").click(function () {

    var item = $("#item").val();
    var quantity = $("#quantity").val();
    var location = $("#location").val();
    var price = $("#price").val();
    //console.log(item);

    if (item.length < 2) {
        $("#item").addClass("error").removeClass("clear");

    } else {
        $("#item").addClass("clear").removeClass("error");

    }

    if (quantity < 1) {
        $("#quantity").addClass("error").removeClass("clear");

    } else {
        $("#quantity").addClass("clear").removeClass("error");

    }

    ////

    if (location.length < 2) {
        $("#location").addClass("error").removeClass("clear");

    } else {
        $("#location").addClass("clear").removeClass("error");

    }


    if (price < 1) {
        $("#price").addClass("error").removeClass("clear");

    } else {
        $("#price").addClass("clear").removeClass("error");
    }


});

});

$(document).ready(function () {

}); /* $('#newItemForm').on('submit', function (e) { e.preventDefault(); var item = $('form').find('input[type="text"]').val(); $('#tableShell').append('<tr><td>' + text + '</td></tr>') }); */

// $(document).ready(function () { /* var $newItemButton = $("#newItemButton"); var $newItemForm = $("#newItemForm"); var $textInput = $("input:text"); $td = $("table");

$newItemForm.on("submit", function (e) { e.preventDefault(); var text = $("input:text").val(); $td.append("<td>" + text + "</td>"); $("input:text").val(""); updateCount(); }); */