Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Interchnage the children of two divs using a button in js ?

By M.K. Verma · 3 May 2022

Interchnage the children of two divs using a button in js ?

Do you mean smth like this?

 

function handleButtonClicked(type) {

  if (type === 'main') {
    const mainElement = document.getElementById('subele1')
    mainElement.innerHTML += '!'
  }

  if (type === 'sub') {
    const mainElement = document.getElementById('subele2')
    mainElement.innerHTML += '?'
  }
}
<div id="mainWindow">
  <div id="subele1">main Window!</div>
</div>

<div id="subWindow">
  <div id="subele2">sub Window?</div>
</div>

<button id='main' onclick={handleButtonClicked('main')}>main</button>
<button id='sub' onclick={handleButtonClicked('sub')}>sub</button>