0

I'm trying to put a JavaScript variable in the href.

<a href="http://google.com/{$order.paymentBefore}/{$order.guid}">click here</a>

When I am using href, because I want the link to appear as "Click here", it stays in the link as it is --> {$order.paymentBefore}/{$order.guid}, unchanged.

Chuck
  • 998
  • 8
  • 17
  • 30
Nika Kvezereli
  • 25
  • 1
  • 1
  • 2

3 Answers3

7

Use click event for <a> as below

HTML

<a onclick="redirectTo();">click here</a>

JavaScript function

function redirectTo(){
     var parameter1 = $order.paymentBefore; // some thing like this you can set value for 1st Param.
     var parameter2 = $order.guid; // some thing like this you can set value for 2nd Param.
    window.location.href="http://google.com/"+parameter1+"/"+parameter2;

}
prog1011
  • 3,425
  • 3
  • 30
  • 57
1

You can mention href value upto what static value you have and the variable can be given in onclick call. See below example:

    var orderLink= "xyz";
     <a style="font-size:large;align-content:center" 
href="http://<static value>" onclick="location.href = this.href + orderLink; return false;"> Click Here </a>
soorapadman
  • 4,451
  • 7
  • 35
  • 47
Vijaya
  • 11
  • 1
0

In your javascript, include a function that changes the href of to what you want. You cannot use it the way you have.

Let your html tag look something like this.

<a id="hyperlink" href="#">Click Here</a>

Now create a javascript function which updates the url whenever you call it.

function myFunction() { //Call this function whenever you want to update the href
    var link = document.getElementById("hyperlink");
    link.href = "http://google.com/" + $order.paymentBefore + "/" + $order.guid;
}
Anirudh Ajith
  • 887
  • 1
  • 5
  • 15