I have Button A with href "www.A.com".
Im trying to figure out condition to achieve this:
If button’s href != "www.A.com", then function rewritte button’s href to "www.A.com".
I imagine whole function like this:
<script>
var x = document.getElementById("button A");
var y = "www.A.com";
window.onload = function() {
if (x.href != y){
document.getElementById("button A").href="www.A.com";
}
}
</script>
Im kind of new to coding in JavaScript, so maybe I’m absolutely wrong.
Thanks for help.
2
Answers
welcome to stack overflow, Your code looks almost correct, but the id shouldn’t have a space
button A
try to put it asbuttonA
and
href
returnning full url includinghttp://
so you shoud make your y variable:and inside load function:
document.getElementById("button A").href= y;
You should not use any space in id, Try changing
button A
intobuttonA
Check out this, it may solve your problem.