javascriptjqueryonclickautolink

js How to add href + text onclick


I need to pass (using javascript) text inside span to href

<div class='tableCell'><span>information</span></div>
<div class='tableCell'><span>contact</span></div>
<div class='tableCell'><span>about</span></div>  

for example when i click to about link must be example.com/tag/about/


Solution

  • The simplest way:

    $( "span" ).click(function() {
     var link = 'http://yousite.com/tag/'+ $(this).text().replace(/ /, "-")+"/";
     window.location.href= link.toLowerCase();
     });
    

    DEMO

    http://codepen.io/tuga/pen/yNyYPM