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.

Share:
17,458

Related videos on Youtube

masteringprojects
Author by

masteringprojects

Updated on September 26, 2022

Comments

  • masteringprojects
    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
      Mussser over 10 years
      What do you mean 'act like a button'? What features do you want specifically?
  • masteringprojects
    masteringprojects over 10 years
    no 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
    João Pinho over 10 years
    @masteringprojects can you use just an <a href=""></a> ??
  • João Pinho
    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
    masteringprojects over 10 years
    no 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
    masteringprojects over 10 years
    This 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
    kba over 10 years
    That neither behaves nor looks like a button.
  • masteringprojects
    masteringprojects over 10 years
    No, 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
    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
    user3152069 over 10 years
    Would 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
    masteringprojects over 10 years
    Yes it is working I have test in on jsfiddle working perfectly ! :) jsfiddle.net/neWuc Thanks to João Pinho!
  • masteringprojects
    masteringprojects over 10 years
    Thank you VERY MUCH João Pinho ! Sorry that I can't vote up ! :)