Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
stop resetting select input whenever new select option is added when button is click

I successfully create new select if the button is click. but how do i stop the selected being refresh whenever I add another select?

it's kind hard for me to explain but here's the code:

<body>
<table>
    <tr>
        <td class="here" colspan="2"> </td>
</tr>
    <tr>
        <td colspan="2" ><a style="color: #4F7942; cursor: pointer;" onclick="add()">Add Select</a></td>
    </tr>
</table>
function add(){
var container = document.querySelector(".here");

if(container){
    container.innerHTML += `
    <table>
        <tr>
            <td>
                <select class="abc" name="any[]">
                    <option value="" selected disabled>Select....</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                </select>
            </td>
        </tr>
    </table>
    `;
}

}

https://jsfiddle.net/idfrnffnd/akjpLc2y/4/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should not alter innerHTMl, instead, use container.insertAdjacentHTML

function add(){
    var container = document.querySelector(".here");
    
    if(container){
        container.insertAdjacentHTML('beforeend', `
        <table>
          <tr>
              <td>
                <select class="abc" name="any[]">
                  <option value="" selected disabled>Select....</option>
                  <option>1</option>
                  <option>2</option>
                  <option>3</option>
              </td>
            </tr>
        </table>
        `);
    }
}
<table>
  <tr>
      <td class="here" colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2" ><a style="color: #4F7942; cursor: pointer;" onclick="add()">Add Select</a></td>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!