How to Add "Href" Attribute to a Link Dynamically Using JavaScript

How can I add href attribute to a link dynamically using JavaScript?

var a = document.getElementById('yourlinkId'); //or grab it by tagname etc
a.href = "somelink url"

Setting href attribute dynamically using js

try like this.

var a = document.createElement("a");
a.setAttribute('href',`doWork(${money})`);

dynamically passing href in anchor tag

Update your JavaScript function like this:

function check_role()
{
var r=document.getElementById('role').value;
alert(r);
location.href = r;
}

How to change href attribute using JavaScript after opening the link in a new window?

Your onclick fires before the href so it will change before the page is opened, you need to make the function handle the window opening like so:

function changeLink() {
var link = document.getElementById("mylink");

window.open(
link.href,
'_blank'
);

link.innerHTML = "facebook";
link.setAttribute('href', "http://facebook.com");

return false;
}

How can i add href dynamic inside link tag

You can't set the href directly like that, because document.getElementsByTagName returns all the tags (as a NodeList). If you're positive you only have one, use this:

var findlink = document.getElementsByTagName("link");
findlink[0].href = url;

If you have multiple link elements and want to target a specific one, give it an id and use document.getElementById:

var findlink = document.getElementsById("myLinkId");
findlink.href = url;

How can I dynamically add an HREF as a field is filled out?

It is a typo. document.getElementByID should be document.getElementById. You can easily spot it from the console.

37:27 Uncaught TypeError: document.getElementByID is not a function

Working fiddle:

https://jsfiddle.net/61wcahxL/3/

How to make the 'href' attribute in HTML dynamic?

How to make the 'href' attribute in HTML dynamic?

Well, you have a variable with the value you want …

absolutePath= "http://localhost:4502/crx/de/index.jsp#"+nodePaths.get(i);
String start = "<a href='' onclick='fns()'>";

… so just use it:

String start = "<a href='" + absolutePath +"'>";


Related Topics



Leave a reply



Submit