Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery hyperlinks - href value?

On my website I use jQuery to hook the events of elements, namely hyperlinks. As these hyperlinks only perform actions on the current page, and do not lead anywhere, I have been putting a href attribute of "#" in:

<a href="#">My Link</a> 

However in some browsers this causes the page to scroll right to top which is obviously undesirable behaviour. I've tried using a blank href value, or not including one, but then the mouse does not change to the hand cursor upon hovering.

What should I put in there?

like image 766
Tom Avatar asked Apr 06 '09 10:04

Tom


People also ask

How do you assign a value to a hyperlink?

Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

Can we use value attribute in anchor tag?

you can use custom data attributes see this . <a href="#" data-json="{ 'myValue':'1'}">Click</a> //you can even pass multiple values there.


1 Answers

$('a').click(function (event)  {       event.preventDefault();       //here you can also do all sort of things  }); 

Then you can put in every href whatever you want and jQuery will trigger the preventDefault() method and you will not be redirected to that place.

like image 75
Bogdan Constantinescu Avatar answered Sep 24 '22 10:09

Bogdan Constantinescu