Switch on / off Links on the page with one button: toggle() two functions

javascript toggle button
javascript toggle class onclick
html toggle button with text
javascript show/hide div onclick toggle
toggle class javascript vanilla
classlist.toggle not working
toggle jquery
toggle function javascript

I've written a working code for the two buttons:

HTML:

<button class="OFF-LINK">OFF</button>
<button class="ON-LINK">ON</button>

<div class="box">...<a></a>...<a></a>...</div>
<div class="box">...<a></a>...<a></a>...</div>

Script:

$(".OFF-LINK").click(function off() {
    var box = document.getElementsByClassName("box"); 
    var x; for (x = 0; x < box.length; x++) 
    {box[x].innerHTML = box[x].innerHTML.replace( /href/ig,'hren');}
}); ///links stop working

$(".ON-LINK").click(function on() {
    var box = document.getElementsByClassName("box"); 
    var y; for (y = 0; y < box.length; y++) 
    {box[y].innerHTML = box[y].innerHTML.replace( /hren/ig,'href');}
}); ///links work again

How can I combine this two functions, to toggle them with one button?

You can achieve this by checking the returned match from RegEx pattern inside .replace() and swap it as per the below snippet

function toggleLinks() {
    var box = document.getElementsByClassName("box"); 
    var x;
    for (x = 0; x < box.length; x++) {
      box[x].innerHTML = box[x].innerHTML.replace(/hre(f|n)/gi,
          g1 => {return (g1=="href") ? "hren" : "href"});  
    }
}
<button onclick="toggleLinks()">Toggle Links</button>

<div class="box">
  <a href="http://www.google.com">google</a>
  <br>
  <a href="http://www.yahoo.com" >yahoo</a>
</div>
<div class="box">
  <a href="http://www.stackoverflow.com"> stackoverflow</a>
  <br>
  <a href="http://www.github.com" >github</a>
</div>

How To Toggle Text, You can achieve this by checking the returned match from RegEx pattern inside .​replace() and swap it as per the below snippet. function  A toggle button is used to indicate a state, such as Yes/No or a mode such as On/Off. The button alternates between an enable and disable state when it is clicked. For example you can use the toggle button to hide and unhide rows, hide and unhide columns, hide and unhide a chart, switch between design mode and edit mode or as an alternative to check box.

I put some text in so you could see the changes

$(".OFF-LINK,.ON-LINK").on('click', function() {
  var box = document.getElementsByClassName("box");
  var x;
  for (x = 0; x < box.length; x++) {
    box[x].innerHTML = $(this).is('.OFF-LINK') ? box[x].innerHTML.replace(/href/ig, 'hren') : box[x].innerHTML.replace(/hren/ig, 'href');
  }
});
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="OFF-LINK">OFF</button>
<button class="ON-LINK">ON</button>

<div class="box">...
  <a>href</a>...
  <a>X</a>...
</div>
<div class="box">...
  <a>href</a>...
  <a>Y</a>...
</div>

How To Toggle Between Class Names, function myFunction() { var x = document.getElementById("myDIV"); if (x.​innerHTML === "Hello") { x.innerHTML = "Swapped text!"; } else { x.innerHTML = "​Hello"; } Connect a jumper wire from one side of the button to pin 2 on the Arduino. Connect a jumper wire from the other side of the button to the ground rail on the breadboard. That’s it for the circuit setup. Now, when you press the push button (which will electrically connect both sides of the button), pin 2 to will have ground voltage applied.

If I'm understanding you correctly, you want to toggle the class on the button between on-link and off-link, and call the appropriate function afterwards? If so:

<button class="button off-link">Off</button>

$('body').on('click', '.button', function(){
  $(this).toggleClass('off-link on-link');
  if ($(this).hasClass('off-link')) {
    $(this).innerHTML('Off');
    // Do your 'off-link' code here
  } else {
    $(this).innerHTML('On');
    // Do your 'on-link' code here
  }
});

.toggle(), Click the button to toggle class name! Toggle Class. Step 1) Add HTML: Toggle between adding a class name to the div element with id="myDIV  Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Full Page Tabs Hover Tabs Top Navigation Responsive Topnav Navbar with Icons Search Menu Search Bar Fixed Sidebar Side Navigation Responsive Sidebar Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Sidebar with Icons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Right Aligned Menu Links Centered Menu Link Equal Width Menu Links Fixed Menu Slide Down Bar on Scroll Hide

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>

WebD2: Using Javascript to Hide and Show Content, Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks. A function to execute every even time the element is clicked​. The .toggle() method binds a handler for the click event, so the rules outlined preventDefault() on the event, so links will not be followed and buttons will not​  Today, I’d like to share with your simple steps to create an on-off button in Excel. I have used this button to create a greeting card for Christmas. So, let’s start our hunt for an on-off switch/button. It can help you to control two macros with one button. You can also use it to start or stop a macro. It should change its position on click.

Javascript · Bootstrap, One way that JavaScript is commonly used is to hide or display content based on For example, a user may select an option when filling out an on-line form, and their In this lesson, you will hide your clock, then add a link that if clicked, will have a button that, when clicked, calls the toggleClock() function, you need to  Switches used in boats can also be classified according to design. They will still employ similar contact mechanism. However, the mode of switching On/Off, the duration of staying On will vary. The following are the common types of switches: 1. Toggle Switch: Toggle boat switches are simple On/Off switches. They comprise of a small lever that

Microsoft Office 2013: Post Advanced, All public APIs are single, chainable methods, and return the collection acted upon. $(".btn.danger").button("toggle").addClass("fat"). All methods should accept​  The second button is to toggle the picture off and has the message “HIDE PICTURE”. a. Make the object which is to be shown and hidden, in our case a picture, into a button. To do this open the Buttons panel (Window > Interactive > Buttons). While the object is selected, click [Normal] in the Appearance section,

ARIA: button role, 2. Ifyou want all revisions and comments to appear in balloons, tap or click the indicator or button or keyboard shortcut functions as a toggle, turning the track 1 To turn the track changes feature off, tap or click the TRACk CHANgES the Reviewing Task Pane 3 link excel worksheet to word document | 4 chart word table  A control that the user can turn on or off by moving its handle. A toggle is designed for recent GUIs but behaves the same way as a check box. Default – The initial value of a control before it is changed by the user. Value – The value of an input control. AccessibleLabel – Label for screen readers. BorderColor – The color of a control