Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:30:56+00:00 2026-05-20T09:30:56+00:00

hey guys, I have an input field that looks for matched characters on a

  • 0

hey guys,
I have an input field that looks for matched characters on a page. This page simply lists anchor links. So when typing I constantly load() (using the jquery load() method) this page with all the links and I check for a matched set of characters. If a matched link is found it’s displayed to the user. However all those links should have e.preventDefault() on them.

It simply won’t work. #found is the container that shows the matched elements. All links that are clicked should have preventDefault() on them.

edit:

/*Animated scroll for anchorlinks*/
var anchor = '',
    pageOffset = '',
    viewOffset = 30,
    scrollPos = '';
$(function() {
    $("a[href*='#']").each(function() {
        $(this).addClass('anchorLink');
        $(this).bind('click', function(e) {
            e.preventDefault();
            //console.log('test');
            anchor = $(this).attr('href').split('#')[1];

            pageOffset = $("#"+anchor).offset();

            scrollPos = pageOffset.top - viewOffset;
            $('html, body').animate({scrollTop:scrollPos}, '500');
        })      
    });
});

Well, I’m looking for all href’s that contain a #. So I know those elements are anchors that jump to other elements. I don’t want my page to jump, but rather scroll smoothly to this element with this specific #id.

This works fine when I use bind('click', ... for normal page-elements that have been loaded when the page is opened. It doesn’t work for anchors that have been loaded via ajax! If I change the bind to live nothing does change for the ajax loaded elements – they still don’t work. However normal anchors that have always been on the page are not triggering the function as well. So nothing works with live()!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:30:56+00:00Added an answer on May 20, 2026 at 9:30 am

    When you say “it won’t work” do you mean that your function is not been called or that you can not cancel out of the function? As far as I know you can not cancel out live events. With jQuery 1.4 you can use return false to cancel out live event propagation. Calling e.preventDefault() won’t work.

    Edit: right so this code should in principal work. What it still won’t do is, it won’t add the ‘anchorLink’ class to your new anchors. However if the clicks work then let me know and I will give you the right code to add the class too!

    var anchor = '',
        pageOffset = '',
        viewOffset = 30,
        scrollPos = '';
    $(function() {
        $("a[href*='#']").each(function() {
            $(this).addClass('anchorLink');
        });
    
    
        $("a").live('click', function(e) {
            if ($(this).attr("href").indexOf("#") > -1) {
                e.preventDefault();
                //console.log('test');
                anchor = $(this).attr('href').split('#')[1];
    
                pageOffset = $("#" + anchor).offset();
    
                scrollPos = pageOffset.top - viewOffset;
                $('html, body').animate({ scrollTop: scrollPos }, '500');
                //nikhil: e.preventDefault() might not work, try return false here
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, unusual question: I have a input field #birthday that should accept european
Hey guys I have an admin page that checks if you are admin before
Hey guys, I have a search field that is not submitting when the enter
Hey guys so, im doing this form with a input that will contain the
Hey guys I have one more question lol. I am using a script that
Hey guys I have an assigned array from mysql results and I simply want
Hey guys I have a query that currently finds the latest comment for each
Hey guys, I have developed a small site that i would like to embed
Hey guys, I have a program that uses ajax to send a post to
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.