You are viewing limited content. For full access, please sign in.

Question

Question

JS Confirm Popup is Executing on Form Open

asked on June 29, 2022

Using javascript, there is a confirm statement within an IF statement. The problem is that the confirm statement executes on Form Open. Why?

$(document).ready(function(){

//Check the "Provide Names" box and clear all "Attendee Name(s)" if No
  $('.provideNames input').change(function () {
    if ($(this).val() == "No")
    {
      // clear all Attendee Name(s)
      return 'WARNING:  By selecting "No", the names already inputted will be cleared. Choose OK to clear the names or Cancel to leave as is.';
      $('.attendeeName input').val("")
    }
  });
  $(".provideNames input:checked").each(function(){  
    $(this).trigger('change');
  });

});  //close document.ready

 

0 0

Answer

SELECTED ANSWER
replied on June 29, 2022 Show version history

Lines 4-11 of your code is telling it what to do when changes happen to the field with provideNames class.
Lines 12-14 of your code is running immediately upon form load - it goes through all checked items with the provideNames class - and for each one found it triggers the change event on that item. 
Since lines 4-11 happen based on the change event, which was just triggered, if any items are checked when your form is loaded, the code in lines 4-11 is run immediately.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.