0

I am trying to format existing href attribute based on click event. Below is mys code.

var clicked = false;
$('a').click(function(e) {
if(clicked === true){
//link already formatted
// link should open here
}
else{
 e.preventDefault();
//code to format link
clicked = true;
$(this).trigger('click');
}
});

Now the issue is, I am able to format the link and then trigger click event; but link is not opening.

Kindly help/guide.

Note : question/query is more towards triggering the click event with modified link.. this part is not explained/answered in any existing threads.

Jacke
  • 1
  • 2
  • Show your markup and what you actually doing to format please. – TheValyreanGroup Dec 27 '16 at 20:33
  • below is code in else logic.. – Jacke Dec 27 '16 at 20:39
  • @Alexander O'Mara. The accepted answer from the question you marked as duplicate uses the same technique as OP. I think the problem is not in _how to trigger the click event in an anchor_ – Washington Guedes Dec 27 '16 at 20:39
  • @Guedes The key part of the accepted answer: *"Beware though, that even in the event gets fired, the browser will not follow the link href."* That's why *"link is not opening"*. – Alexander O'Mara Dec 27 '16 at 20:40
  • below is code to format href... else{ e.preventDefault(); //code to format link var pathName = location.pathname; var target = $(this).attr("href") + "?source=" + pathName ; $(this).attr('href',target); console.log($(this).attr("href")); console.log("prevent default"); clicked = true; $(this).trigger('click'); } – Jacke Dec 27 '16 at 20:40
  • @AlexanderO'Mara. Does it mean that `.trigger('click')` is an untrusted event? Or something like that? Anyways, how does this solves OP problem? – Washington Guedes Dec 27 '16 at 20:42
  • @Guedes Yes, it's untrusted when manually triggered. #2 answer suggests using `window.open` instead. – Alexander O'Mara Dec 27 '16 at 20:43
  • Hm ok... @Jacke. Try `location.href = this.href;` instead of `$(this).trigger('click');` – Washington Guedes Dec 27 '16 at 20:44
  • @Guedes- thanks a lot, that worked..but its not working for #link. – Jacke Dec 27 '16 at 20:54

0 Answers0