1

I've got a newbie jQuery question. I need to add a number of tiny text boxes with links to scroll up and down any overflowed content.

I've got it to work as follows, but need to rework it to handle repeated boxes of text on the page.

Javascript..

$(function() {
    $('a.scrolldown').click(function(event) {   
    event.preventDefault();
    $(".mytextbox").scrollTop($(".mytextbox").scrollTop() - 20);
    });
});

With the markup...

<div class="news_col">
    <p class="newscontrols">
     <a class="scrolldown" href="#">down </a><a class="scrollup" href="#">up</a></p>
     <div class="news_item_text mytextbox">
      <p>Loren ipsum...</p>
     </div>
</div>

I realise I could use maybe the 'next' selector but need a bit of help rewriting it.

1 Answer 1

1

You can use next() with $(this) to access the textbox after the .scrolldown anchor.

$(function() {
    $('a.scrolldown').click(function(event) {   
       event.preventDefault();
       $mytextbox = $(this).next(".mytextbox");
       $mytextbox.scrollTop($mytextbox.scrollTop() - 20);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Tweaked the markup with 'up' and 'down' <a> tags before and after the text container, using next() and prev().

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.