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
Jquery: How to Listen for Dom Changes
Why Aren't Some Technically Serializable Input Properties Serializable
Decoding Url Parameters with JavaScript
How to Format Vuetify Data Table Date Column
How to Mute All Sound in a Page with Js
Python Library for Rendering HTML and JavaScript
Angular2 Router 2.0.0 Not Reloading Components When Same Url Loaded with Different Parameters
How to Disable the Save Password Bubble in Chrome Using JavaScript
When Using Jquery On(), Why Use (Document) VS. the Element Itself
:Touch CSS Pseudo-Class or Something Similar
How to Detect Overflow in Div Element
Bootstrap and Jqueryui Conflict
How to Get the Position of Element Transformed with CSS Rotate
Differencebetween Angular-Route and Angular-Ui-Router