Reputation: 4564
<li> <a href="#calculate">Calculate</a></li>
<li> <a href="#calculate1">Calculate1</a></li>
<li> <a href="#calculate2">Calculate2</a></li>
this is the code I have, when I click on the link the page scrolls down to where #calculate is,which is understandable. But I need it to not scroll I can remove the value of the href,is there anyway to block it from scrolling WITHOUT removing the value #calculate and without changing the id names?
Upvotes: 0
Views: 441
Reputation: 97717
Just prevent the default action of the link
$('li > a[href^=#calculate]').on('click', function(e){
e.preventDefault();
return false;
});
'li > a[href^=#calculate]'
selects an a
element whose href attribute begins with #calculate
that is a child of an li
element
Upvotes: 1
Reputation: 87073
$('li > a[href*=calculate]').on('click', function(e) {
e.preventDefault(); // this will prevent the page scroll
// continue other code
});
Read about .preventDefault()
Here, 'li > a[href*=calculate]'
will select those anchor tags which are direct(first level) children of li
and contains calculate
word in their href
attribute.
Upvotes: 2
Reputation: 14187
In modern jQuery versions:
$('a').on('click', function(e) {
e.preventDefault();
});
Other jQuery versions:
$('a').click(function() {
return false;
});
Upvotes: 2