0

As href argument, I would like to put a variable.

Instead of:

<p><a href="http://www.google.com">Go to Google</a></p>

</body>
</html>

I would like to write something as:

<html>
<body>

var link = "http://www.google.com"
<p><a href=link>Go to Google</a></p>

</body>
</html>

2 Answers2

1

You can do something like:

<html>
<head>
</head>

<body>
<p><a id="some-link">Some link</a> is here</p>

<script>
  var link = 'http://www.google.com';

  var someLink = document.querySelector('#some-link');
  someLink.setAttribute('href', link);
<script>
</body>
</html>

The script tag executes javascript actions. This one specifically sets the href of the given item with the some-link id. The # means "element with the id".

You can try it over here. https://jsfiddle.net/94ct5hvr/

user3254198
  • 713
  • 6
  • 22
-1

You can do this with angularJS

<html>
 <body ng-app="App" ng-controller="myApp">

<p><a href={{link}}>Go to Google</a></p>

</body> 
</html>

On the javascript side you can do:

var app = angular.module('App',[]);

app.controller(function($scope){
   $scope.link = "http://www.google.com;
});
mfcastro
  • 35
  • 5