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

Angelic Sanoy
seal-mask
.a{fill-rule:evenodd;}techdegree
Angelic Sanoy
Full Stack JavaScript Techdegree Student 6,583 Points

Jquery

Any idea why it's not working. I want to a result of 1.5 if they type 2.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.face-button').click(function() {

    var PostCount = parseFloat($('.post_count:input').val());
    var PostValue;

    if (PostCount == 1) {
                PostValue = 1;
                } else if ( PostCount == 2) {
                 PostValue = 1.5;

                } else if ( PostCount == 3) {
                 PostValue = 2;

                }else if ( PostCount == 4) {
                 PostValue = 2.5;

                }else if ( PostCount == 5) {
                 PostValue = 3;

    } else {    

    $('.result').text('Total Cost: $' + PostValue );


      }

}); });

</script>

1 Answer

Steven Parker
Steven Parker
231,269 Points

The selector '.post_count:input' includes a pseudo-class which is not one of the standard ones according to MDN. With no HTML shown I can only guess at a fix. If it's an input element that is inside a container with the "post_count" class, perhaps you want ".post_count input". Or if it's an input with that class, "input.post_count". Otherwise, I'd need to see the HTML.

Then, because of the final "else", the ".result" will only be updated when the input is not recognized and the "PostValue" is still undefined. If you want the update to occur on every click, that code should not be in an "else".