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.

3
  • 2
    What template engine were you expecting was going to parse that variable? WHat langauge are the variables created in Commented Jan 26, 2015 at 10:17
  • wirte a function that generates the href. You can not mix html and js like you did. Commented Jan 26, 2015 at 10:17
  • Look at this post: stackoverflow.com/questions/4772774/… Commented Jan 26, 2015 at 10:18

3 Answers 3

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;

}
Sign up to request clarification or add additional context in comments.

2 Comments

@NikaKvezereli - if above answer is useful for you then accept the answer and upvote it will be helps other to find correct solutions.
This is awesome
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>

Comments

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.