SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    0 Post(s)
    0 Thread(s)

    Need help with jquery focus()

    I have some script that allows a user to "like" an article. If the article is near the bottom of the page results, once the user clicks I would like the page to remain at that specific part of the page, however currently the user clicks "like" and the focus goes to the top of the page..

    How do I use jquery to to focus, do i use focus() or is another approach better for what I am trying to achieve??

    At the moment I am using below code, but that has the scroll effect which I don't want:

    $('html, body').animate({ scrollTop: $('#idea_x_'+idea_id).offset().top }, 'fast');

  2. #2
    SitePoint Enthusiast aufshebung's Avatar
    Join Date
    May 2012
    Santa Fe, NM
    0 Post(s)
    0 Thread(s)
    Focus probably isn't what you're looking for. I hate's JavaScript section, but they do a decent job explaining what "focus" means in JS:

    The focus method is used to give the focus to (ie make current) the object that it belongs to. Giving a text field the focus will move the cursor to that field. Giving a window the focus will move it in front of all of the others.
    But if I understand correctly you aren't trying to change the current object, you're trying to stop the window from scrolling.

    This is just a guess, without seeing the page at all: The "like" button is an <a> element with its `href` attribute set to "#", and the JavaScript that handles the "liking" doesn't prevent the click's default action.

    As I said, just a guess. Can you link to the page in question? If not, can you create/upload/link to an example that demonstrates the problem? If not, can you at least post the markup & code you're using? Then we could track down the bug for sure and provide a definite answer.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts