Generally, it's bad practice to replace all innerHTML, just to change a little detail. If you need to toggle links, would be better to find the required links and work only with their attributes:
$('.box a').each(function(){
var link = $(this).attr("href");
$(this).attr("data-link", link);
// saving each link in data-attribute
});
$('#toggle').on('click', function toggler(){
let on = toggler.on = !toggler.on; // (*1)
$('.box a').each(function(){
var link = $(this).attr("data-link");
$(this)[ on ? "removeAttr" : "attr" ]( "href", link ); // (*2)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="toggle">Toggle Links</button>
<div class="box">
<a href="https://google.com" target="_blank">link</a>
<a href="https://google.com" target="_blank">link</a>
</div>
<div class="box">
<a href="https://google.com" target="_blank">link</a>
<a href="https://google.com" target="_blank">link</a>
</div>
(*1) let on = toggler.on = !toggler.on; — each function is an object. You can work with it's name just like with common objects - setting him new properties or methods. I could declare a variable let on = true; out of the function, but decided to make everything inside. ! — it's the boolean "NOT": transforms false → true and vice versa. So, when you click the button, toggler.on is undefined (false), ! will become true and assign to itself (toggler.on = !toggler.on;) → the same value will be assigned to variable on.
At the next click, property toggler.on already will keep the true value, and become !true → false, and so on.
(*2) $(this)[ on ? "removeAttr" : "attr" ]( "href", link );
• Ternary operator, general form: (boolean expression) ? (return this value if true) : (else)
In this example, expression will return a "removeAttr" string, if on === true, and "attr" otherwise.
• Bracket notation. Mostly we use the dot notation, because it's shorter. Example, elem.innerHTML may be written as elem["innerHTML"]. Here I used a ternary expression, to pick the required method. Translation into if-else:
if( on ){
$(this).removeAttr("href")
} else {
var link = $(this).attr("data-link");
$(this).attr("href", link );
}
And the same code without jQ:
let links = document.querySelectorAll('.box a');
for( let i = 0; i < links.length; i++ ){
links[i].dataset.link = links[i].getAttribute("href");
}
let toggle = document.getElementById('toggle');
toggle.addEventListener('click', function toggler(){
let on = toggler.on = !toggler.on;
for( let i = 0; i < links.length; i++ ){
let link = links[i].dataset.link;
links[i][ on ? "removeAttribute" : "setAttribute"]("href", link);
}
});
<button id="toggle">Toggle Links</button>
<div class="box">
<a href="https://google.com" target="_blank">link</a>
<a href="https://google.com" target="_blank">link</a>
</div>
<div class="box">
<a href="https://google.com" target="_blank">link</a>
<a href="https://google.com" target="_blank">link</a>
</div>