raj raj

raj raj

  • 1.5k
  • 256
  • 18.5k

toggle button

Sep 2 2023 4:06 PM

We provided some simple JavaScript template code. Your goal is to modify the application so that you can properly
\ toggle the button to switch between an ON state and an OFF state. When the button is on and it is clicked, it turns 
off and the text within it changes from ON to OFF and vice versa. 

You are free to add classes and styles, but make sure you leave the element ID's as they are.

Below code i tried

import $ from "jquery";

$(document).ready(function(){
  $("#root").click(function(){
    if (rootApp.innerHTML === '<button>ON</button>'){
      rootApp.innerHTML = '<button>OFF</button>';
    } else {
      rootApp.innerHTML = '<button>ON</button>';
    }
  })
})

const rootApp = document.getElementById("root");
rootApp.innerHTML = '<button>ON</button>';

it is working fine but do we have any other way to do it better way.


Answers (3)