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

137
Views
Select 2 js adding actions on new option

I use Select 2 in my form where the option data in it I take from the database and I also add an "add new" button when the searched keyword is not in the database. what I want to ask is, what kind of way can I do to be able to create an action from the button. for example when the button is clicked I can display a text field.

For illustration, here is my code:

$('#add_penilai').select2({
       placeholder: 'Change Name',
       tags : true,
         createTag: function (params) {
    return {
      id: params.term,
      text: params.term,
      newOption: true
    }
  },
   templateResult: function (data) {
    var $result = $("<span></span>");
    $result.text(data.text);
    if (data.newOption) {
      $result.append(`<button type="button"
            class="btn btn-secondary btn-sm ml-10" 
            onClick='Show()'> + Add New Item</button>`);
    }
    return $result;
  },
        escapeMarkup: function (markup) {
            return markup;
        }
    });
.form-control.form-control-hide {
  display: none;
}
<select class="form-control select2" id="add_penilai" name="nama_penilai" onchange="isi_otomatis()" data-width="100%">
     <option value="Jhony">Jhony</option>
     <option value="Kyra">Kyra</option>
     <option value="Ronald">Ronald</option>
 </select>
 
 <input class="form-control form-control-hide" type="text" id="divisi" name="nama_instansi" />

about 4 years ago · Juan Pablo Isaza
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!