Jump to content

get javascript variable and put it inside a href


robert_gsfame

Recommended Posts

assume i have this javascript code

 

var a="bbb"

 

and i wish to put it inside my dynamic php code

echo "<a href='page1.php&category=_____></a>";

 

i wish to have

<a href='page1.php&category=bbb></a>

where bbb was taken from javascript function

 

is that possible??thx

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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.