Search code examples
javascriptbuttonautodisabled-input

How to disable all bottons on page with javascript if all of them contains specific name like "Not Available"?


I have a page contains many buttons I want to disable all buttons that has name "Not Available" all at once automatically by javascript code Basically I want the code to read the page and disable all buttons with that name even if a button name is "Sorry, Not Available" javascript will target it because it got "Not Available" in it. if buttons doesnt have that name they stay active and not disabled.

I hope my question is clear.


Solution

  • This will do the job

    const buttons = document.querySelectorAll('button')
    
    buttons.forEach((button) => {
      if (button.innerText.match(/Not\sAvailable/gi)) {
        button.setAttribute('disabled', 'true')
      }
    })