How can I add href attributes to <a></a> links dynamically using JavaScript?
<a href="dynamic url">Link</a>
const a = document.getElementById('yourlinkId'); //or grab it by tagname etc
a.href = "somelink url"
setAttribute). Does anybody know if this approach is standard?HTMLLinkElement which supports setting certain fields such as href. You have to look in the reference to see which one you can use without having to setAttribute. Another example is the <table> element (HTMLTableElement) where you can use insertRow() to insert new rows without having to create the <tr> and append it to the table.setAttribute() and getAttribute(), which are broken in IE and don't always do what you might expect. See stackoverflow.com/questions/4456231/…I assume you know how to get the DOM object for the <a> element (use document.getElementById or some other method).
To add any attribute, just use the setAttribute method on the DOM object:
a = document.getElementById(...);
a.setAttribute("href", "somelink url");
setAttribute is clearly the standard (w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082), and W3C doesn't define any attributes it doesn't work for. Conversely, I can guarantee that using the property will fail for certain attribute names. Such as tagName, and setAttribute -- those are already fields/methods of the Element interface. I don't see anywhere in the W3C document which mentions properties for attributes.tagName. Are you talking about custom attributes?getAttribute and setAttribute; "tagName" is an example of an attribute that can't work as a property. Only when working with HTML, and for certain attributes defined in the DOM HTML spec, can you use the properties to read and assign attributes.tagName is a property of Element objects in JavaScript, in both HTML and XML DOMs, and you cannot get an element's tag name via getAttribute("tagName") (except in IE, whose implementation of getAttribute() and setAttribute() is broken), which seems precisely the opposite of what you're saying.More actual solution:
<a id="someId">Link</a>
const a = document.querySelector('#someId');
a.href = 'url';
querySelector is slower than getElementById.First, try changing <a>Link</a> to <span id=test><a>Link</a></span>.
Then, add something like this in the javascript function that you're calling:
var abc = 'somelink';
document.getElementById('test').innerHTML = '<a href="' + abc + '">Link</a>';
This way the link will look like this:
<a href="somelink">Link</a>
href to an existing a tag (I basically want to add a href attribute to <a></a> dynamically). However, this answer seems to be explaining how to create an a tag with an href.element.setAttribute('href', abc)I came here because I wanted to dynamically set the link href after it's been clicked
<a id='dynamic'>link text</a>
document.getElementById("dynamic").addEventListener("click", onClick_dynamic)
function onClick_dynamic(e){
const nextPage = getNextPage()
document.getElementById("dynamic").href = _BASE_URL + "?nextPage=" + nextPage
// e.default processing sends user to href
}
I know there seems plenty good answers here, but none of them seemed simple enough for what I was "told" to do in the 2022 Udemy Web Development Bootcamp by Angela.
So I remembered how simple the use of scriplets was and figured to try it and it works just as well.
For those like me who are learning let me explain: . - takes you to current URL then static path then dynamic variable generated for each post (its a blog website)
<a href="./posts/<%= post.title %>">Read More</a>
This is using JS inside an EJS page
same solution is also given in the solution lecture of the bootcamp here: https://www.udemy.com/course/the-complete-web-development-bootcamp/learn/lecture/12385596#overview
Lecture 317
Single line solution
<a id="yourId">Link</a>
document.querySelector("#yourId").href("URL")
javasicript added
var x = "www.google.com";
vay y = "550";
var z= x+y;
document.write('<a href="' + z + '">GONDER</a>');
document.write is a horrible method that rarely should even be thought about for use.