Question

I'm trying to replicate something I've seen on a few blogs and is somewhat used on Facebook, and that's moving to the next post by pressing a key. On Facebook it's pressing the "j" key. I'm not very good at Javascript, and have found a few articles on here in regards to how to do it, but I feel like I'm missing something. Here's my View code, please show me what I'm doing wrong. The alerts aren't even firing. Thanks.

@{
    ViewBag.Title = "Index";
}

<script>
    $(document).keyup(function (e) {
        alert("entered function");
        if (e.keyCode == 90 || e.keyCode == 16) {
            alert("entered conditional");
            var ta = document.getElementsByClassName('post_unread')[0];
            ta.removeAttribute('class', 'post_unread');
            ta.addAttribute('class', 'post_reading');
        }
    });
</script>

<div>
    <div class="forum_post_wrapper">
        <div class="forum_post">

        </div>
        <div class="forum_post_wrapper">
            <div class="forum_post post_unread">

            </div>
            <div class="forum_post_wrapper">
                <div class="forum_post post_unread">

                </div>
            </div>
        </div>
    </div>
</div>
Was it helpful?

Solution

Have you imported the jQuery file, the reason why I ask is you've not tagged with jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" />

Maybe you missed to add this script.

$(document).keyup(function (e) { // this belongs to jQuery
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top