Jump to content

get javascript variable and put it inside a href


robert_gsfame

Recommended Posts

Expand the scope of the javascript function to return the entire href.

 


<a href="javascript:getHref('page1.php&category',a)">

function getHref(x,y){
return \"+ x+"="+y+\"
}

 

you don't need this much overhead but it shows you a solid way to understand what is going on.

 

Link to comment
Share on other sites

Ugh, that's ugly.  A much cleaner version:

 

<!DOCTYPE html>
<html>
   <head></head>

   <body>
      <a id="linkToChange" href=""></a>
   </body>

   <script type="text/javascript">
      function generateLink(/* argument list, if necessary */) {
         // do whatever you need to generate the link's href and text
         var link = document.getElementById('linkToChange');
         link.href = // something
         link.innerHTML = // text
      }
   </script>

</html>

 

Not a fully fleshed out example, as it depends on what you actually need to do to generate the link info, to say nothing about the number of links you need to populate.  Still, much cleaner than adding the JS directly in the link's href attribute.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.