Search code examples
javascriptsweetalertsweetalert2

set value of custom input before alert popup


swal2({
  title: 'Messages',
  html: '<textarea id="text_messages" class="inpz textar" oninput="set_messages()" ></textarea>'
})

the problem is I need to set the value of textarea when it popup so when it shows it should already have the value I tried onfocus event for the textarea is working but need to focus to show the value, and I tried 'preConfirm'but I think its only trigger before html code load any idea of what I'm doing wrong


Solution

  • swal({
      html: '<textarea id="text_messages" class="swal2-textarea" rows="5"></textarea>',
      onOpen: function () {
        swal.getPopup().querySelector('#text_messages').value = 'custom value'
      }
    })
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script>