0

I've been trying this for a while and I cant find a solution, hope you can help.

I have a link to a youtube video like this

    <a id='LinkClick1' href='www.youtube.com' target='_blank'><strong>Title</strong></a>

and I wish to register when that link is clicked, but everytime I click it will open the website in another tab and wont do what I have in my function

    $("#LinkClick1").click( function(){
      var a='Hi!';
      var clicked='yes';
      console.log(a+clicked);
    });

How Can i make my link click to open the site but also trigger a function?

Thank you.

12
  • 2
    Your code does exactly that: jsfiddle.net/tymeJV/CE2k5 Commented May 30, 2013 at 15:10
  • I don't see a function call in your click function other than console.log. Are you saying that console.log isn't firing when you click the link? Commented May 30, 2013 at 15:10
  • 3
    I suspect that your code to assign the "click" handler is running before the <a> element is added to the DOM. Move your <script> to the very end of the <body>. Commented May 30, 2013 at 15:11
  • 1
    add your code inside $(document).ready(function(){..} Commented May 30, 2013 at 15:12
  • does your link is added at runtime (by ajax)? Commented May 30, 2013 at 15:15

1 Answer 1

1

Another DEMO... with Link inside popover as mentioned in comment in Question.

http://jsfiddle.net/weuWk/1126/

Related code..

var img = '<a href="https://si0.twimg.com/a/1339639284/images/three_circles/twitter-bird-white-on-blue.png" target="_blank" id="1">TEST</a>';
$("#blob").popover({
  trigger: "manual",
content: img    
}).on("click", function(e) {
  e.preventDefault();
}).on("mouseenter", function() {
  $(this).popover("show");
  $(this).siblings(".popover").on("mouseleave", function() {
    $(this).hide();
  });
}).on("mouseleave", function() {
  var _this = this;
  setTimeout(function() {
    if (!$(".popover:hover").length) {
      $(_this).popover("hide")
    }
  }, 100);
});

$(document).on('click', '#1',function(){
console.log("CLicked");
});

Update considering the bootrstrap popover. If its still not working for you, better show HTML or even better show your problem DEMO on jsfiddle.

http://jsfiddle.net/AFffL/547/

<a class="popup-marker btn" data-content="Click outside the popover to close it." data-original-title="Popover A" href="youtube.com" target="_blank">Click me (A)</a>

jQuery(function() {


    $('.popup-marker').popover().on('click', function(e) {
        // if any other popovers are visible, hide them
        var a='Hi!';
       var clicked='yes';
      console.log(a+clicked);
    });


});

_----------------------------------------------------

The code you have shown does do exactly what you saying... See demo at

http://jsfiddle.net/tymeJV/CE2k5/ (.. By tymeJV in comment)...

However if the link is loaded at runtime by dom manipulation or by Ajax, then your code might not work. In that case try below code.

$(document).on('click','#LinkClick1',function(){
      var a='Hi!';
      var clicked='yes';
      console.log(a+clicked);
    });

Instead of $(document) you can use the Selector for parent of the #LinkClick1

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for event delegation instead of the cookie cutter "throw it in a .ready()"

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.