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

  • SEARCH
  • Home
  • 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 9193087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:04:40+00:00 2026-06-17T21:04:40+00:00

i have a link when clicked it creates a div with another link inside

  • 0

i have a link when clicked it creates a div with another link inside it i want to disable the new link tried many solutions like preventDefault and return false but didn’t work and here’s the example http://jsfiddle.net/2RSJj/1/:

$(document).ready(function(){
$("a[title='go']").click(function(e){
    $(".cities").append("<div class='city'><a href='http://www.google.com' class='mylink'>google</aiv>"); 
    e.preventDefault();
});

$(".cities .city a").click(function(e){
    e.preventDefault();
    alert($(this).attr("class"));
});

});

main html :

<a href="#" title="go">go</a><p>
<div class="cities">

</div>
  • 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-06-17T21:04:41+00:00Added an answer on June 17, 2026 at 9:04 pm

    As the link is dynamically added, you should use the on() method (assuming jQuery >= 1.7; jQuery < 1.7 should use delegate()) to prevent the default action of said link:

    $('.cities').on('click', 'a.mylink', function(e){
        e.preventDefault();
    });
    

    JS Fiddle demo.

    The problem you were having is that the link isn’t present when the event-binding happens, therefore the click() method doesn’t bind the click event to the relevant a element. With on(), the click is attached to a parent/ancestor element (that exists at the point of event-binding), and then listens for that event (the 'click' string in the first argument) being fired on an element that matches the selector (passed as the second argument).

    Assuming that both the event and the selector match (the a.mylink element is click-ed), then the function is executed, which in this case simply prevents the default action being fired. It is, though, important to note that with delegated events you cannot simply return false, because the event has, by definition, already been fired by the time it’s propagated/bubbled up to the ancestor element to which the event is bound.

    Also, if an element between the two elements has an event handler that explicitly implements return false then the event delegation won’t progress past that element (since the click will propagate/bubble to the element with the handler, which will respond with the return false (a combination of event.stopPropagation() and event.preventDefault()).

    For example, in the following code, I’m using the html:

    <a href="#" title="go">go</a><p>
    <div class="cities">
        <div class="demo">
        </div>
    </div>
    

    In this case the on() method remains attached to the .cities element, but the .demo element has a click-handler which simply, as you might imagine, returns false:

    $("a[title='go']").click(function(e){
        $(".demo").append("<div class='city'><a href='http://www.google.com' class='mylink'>google</a></div>"); 
        e.preventDefault();
    });
    
    $('.cities').on('click', 'a.mylink', function(e){
        e.preventDefault();
        // using an alert because a simple 'e.preventDefault()` wouldn't
        // be visibly different to the 'return false'
        alert(e.type + ' event reached the ".cities" element!');
    });
    
    $('.demo').click(function(e){
        return false;
    });
    

    JS Fiddle demo.

    In the above note that there is no alert fired when clicking the a.mylink element, whereas without that click-handler intercepting (and negating) the click event, the alert() is fired:

    $("a[title='go']").click(function(e){
        $(".demo").append("<div class='city'><a href='http://www.google.com' class='mylink'>google</a></div>"); 
        e.preventDefault();
    });
    
    $('.cities').on('click', 'a.mylink', function(e){
        e.preventDefault();
        alert(e.type + ' event reached the ".cities" element!');
    });
    

    JS Fiddle demo.

    So, be aware of those events bound to your elements when using delegation. It can be tricky (ish), when starting.

    Reference:

    • delegate().
    • on().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am trying to have a div slideToggle when this link is clicked.
I have a href link and I would like it to be clicked when
I have the following ajax request that creates a link that, when clicked, replaces
I have link that calls a function when clicked: <a href=javascript:spawnMenu(this); id=link1>Test1</a> To make
I have a link that, when clicked, should deliver an email by using an
I have a page which when an a link is clicked (which must remain
I have some jQuery code that highlights a link when clicked, and changes the
I have some code right now that executes when a link is clicked. The
So, I have a link at the bottom of website. When it is clicked,
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same

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.