To change the attributes of HTML elements using JavaScript, you can use the setAttribute()
and getAttribute()
methods. Here's an example of how to change the src
attribute of an image element and the href
attribute of a link element:
getElementById()
, getElementsByClassName()
, getElementsByTagName()
, or other DOM manipulation methods. For example:var image = document.getElementById("imageId");
var link = document.getElementsByClassName("linkClass")[0];
setAttribute()
method. For example:image.setAttribute("src", "newImage.jpg");
link.setAttribute("href", "https://example.com");
getAttribute()
method. For example:var currentSrc = image.getAttribute("src");
console.log("Current src: " + currentSrc);
var currentHref = link.getAttribute("href");
console.log("Current href: " + currentHref);
removeAttribute()
method. For example:link.removeAttribute("href");
Note: Make sure to replace "imageId"
with the actual id
of the image element and "linkClass"
with the actual class name of the link element you want to modify.