-3

I am wondering how I can add html text inside tag using javascript. Not class or id that can be added with classList.add('') or setAttribute. For example, I want to add html text 'disabled' inside the button tag like this: <button class="button is-danger" id="reset">Reset</button> to <button class="button is-danger" id="reset" **disabled**>Reset</button>

What would be the code for javascript? Thank you very much in advance

Edit: Disabled is an attribute

2 Answers2

0

Disabled too is an attribute. Just writing disabled is the same as defaulting it to true.disabled

If you want to add a custom attribute to pick you can and should refer to data attributes here:custom attributes

As an example, you can add these attributes via simple dom-manipulation using setAttribute itself.setting an attribute

Aditya Rastogi
  • 376
  • 2
  • 9
0

disabled is also an attribute i.e boolean attribute. If the attribute is present irrespective of the value, its value is always true if any boolean attribute is present. StackOverflow

const first = document.querySelector("#first");
const second = document.querySelector("#second");
const third = document.querySelector("#third");
const fourth = document.querySelector("#fourth");

second.addEventListener('click', () => {
  first.setAttribute('disabled', true);
})

third.addEventListener('click', () => {
  first.setAttribute('disabled', false);
})

fourth.addEventListener('click', () => {
  first.removeAttribute('disabled');
})
<button id="first"> Hello world </button>
<button id="second"> click to disable </button>
<button id="third"> click to disable </button>

<p> If you want to enable the button then you have to remove the attribute completely. Setting the value to false doesn't enable it</p>
<button id="fourth"> click to enable </button>
DecPK
  • 24,537
  • 6
  • 26
  • 42