a href link that will act like a button
17,458
Do you mean something like this?
<a id="[8]" class="readmore" href="#">
Click here to read more...
</a>
Adapting your javascript to the new "a":
$("a.readmore").click(function() {
var id=this.id.split('[');
var d_id=id[1].split(']');
var ii=d_id[0] $('html, body').animate({
scrollTop: $('[id='+ii+']').offset().top
}, 2000);
return false;
});
Just a tip, your string processing looks terrible, suggestion:
$("a.readmore").click(function() {
var id=this.id.match('[0-9]+');
$('html, body').animate({
scrollTop: $('[id='+ id +']').offset().top
}, 2000);
return false;
});
Ensure this code is being intercept by the page dom parser in the right time, put it inside a
$(document).ready(function(){ ... });
Regards.
Related videos on Youtube
Author by
masteringprojects
Updated on September 26, 2022Comments
-
masteringprojects over 1 year
Is there any clean method to make a regular link :
<a href="[8]">Click here to read more...</a>
to act EXACTLY like a button
<button id="[8]">Click here to read more...</button>
Thanks !
-
Mussser over 10 yearsWhat do you mean 'act like a button'? What features do you want specifically?
-
-
masteringprojects over 10 yearsno I want to completely remove the button. I make the button looks like a link via css but this is not a clean method.
-
João Pinho over 10 years@masteringprojects can you use just an <a href=""></a> ??
-
João Pinho over 10 years@masteringprojects what do you mean by "with act exactly"? If you add on click return false it will not follow the href link. Therefore, it will behave like a button, assuming you added the appropriate CSS styles. Although, why do you need this, is it somekind of hack over an existing system?
-
masteringprojects over 10 yearsno I have a function that split the [{number}] from the <button id="[8]">Click here to read more...</button> and then it's open (slide to) article number 8 and so on...
-
masteringprojects over 10 yearsThis is the function : $("button").click(function() { var id=this.id.split('['); var d_id=id[1].split(']') var ii=d_id[0] $('html, body').animate({ scrollTop: $('[id='+ii+']').offset().top }, 2000); });
-
kba over 10 yearsThat neither behaves nor looks like a button.
-
masteringprojects over 10 yearsNo, This is definetly NOT what I'm looking. I don't need the link to looks like a button I need the link to behave like a button.
-
João Pinho over 10 years@masteringprojects switch the button to link then, like it is in my answer you code will work, just change the $("button") to $("a"), maybe consider add a class to "a" so you can write something more precise like $("a.readmore").
-
user3152069 over 10 yearsWould you please describe "behave" ? What do you want an onclick event ? You could add an id to the link the and call it trough an external js file, like $("#btn").onclick = function(){ ... }
-
masteringprojects over 10 yearsYes it is working I have test in on jsfiddle working perfectly ! :) jsfiddle.net/neWuc Thanks to João Pinho!
-
masteringprojects over 10 yearsThank you VERY MUCH João Pinho ! Sorry that I can't vote up ! :)