Javascript ES6 and Rails
by grail from LinuxQuestions.org on (#4XC24)
Hoping someone can help me understand if I have made a mistake or not?
I have the following javascript code:
Code:document.addEventListener('DOMContentLoaded', () => {
let link = document.getElementById('be_gone');
if (link) {
link.onclick = () => link.style.display = "none";
}
}, false);When I first arrive on the page associated to the code and click the link it disappears just fine. If I then refresh the page and the link comes back and click it, it once again will disappear.
However, I have another link on the page which links back to the same page:
Code:<a class="btn btn-secondary" href="#">
Yeah <img class="far fa-thumbs-up" src="" />
</a>When I click this link, my disappeared link re-appears but is now not working, ie. when clicked it will no longer disappear.
Would someone be able to tell me if this behaviour is correct or is there a change I can make to the code so it will always work?
Please let me know if any other information is required as I am new to javascript?


I have the following javascript code:
Code:document.addEventListener('DOMContentLoaded', () => {
let link = document.getElementById('be_gone');
if (link) {
link.onclick = () => link.style.display = "none";
}
}, false);When I first arrive on the page associated to the code and click the link it disappears just fine. If I then refresh the page and the link comes back and click it, it once again will disappear.
However, I have another link on the page which links back to the same page:
Code:<a class="btn btn-secondary" href="#">
Yeah <img class="far fa-thumbs-up" src="" />
</a>When I click this link, my disappeared link re-appears but is now not working, ie. when clicked it will no longer disappear.
Would someone be able to tell me if this behaviour is correct or is there a change I can make to the code so it will always work?
Please let me know if any other information is required as I am new to javascript?