-1

I want to put for loop for below function i want loop in "textInput" field so i use same function for different Id's linke textInput1, textInput2,.....

function onButtonClick(){
  document.getElementById('textInput').className="show";
}
jugal
  • 284
  • 1
  • 2
  • 12
  • Sounds like you could do the task by using [event delegation](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation) instead of a bunch of `id`s. If you can show some more context (the relevant markup), we could suggest a suitable code for the delegation. – Teemu May 26 '21 at 06:05
  • Use a class instead of multiple IDs? That's what classes are for – Jeremy Thille May 26 '21 at 06:24

1 Answers1

0

You can pass id in onclick event and use it like below

function onButtonClick(id){
  document.getElementById('textInput'+id).className="show";
}
.show{
display:block
}
.hide{
display:none
}
<input type='button' value='Add' onclick='onButtonClick(1)'>

<div class='show'>show</div>

Or you can also use following but it will run repeatedly when click on button

  function onButtonClick(){
for(let i=0;i<10;i++){
document.getElementById('textInput'+i).className="show";
}
      
    }
Wajid
  • 448
  • 7