2

My goal is to replace a button with another button, but I am running into some issues. I am able to trigger the first button click and I am able to cause an alert with the second button click, but for some reason when I try to trigger the first button click in the click event handler of the second button, it doesn't work. What am I doing wrong? For some context, I'm doing this in Powerapps Portals by adding a Content Snippet.

$(window).load(function() {

     //Code to Add Custom 'Register' Button (and Hide the original one- currently commented out)
     $('#SubmitButton').after('<input type="submit" name="ctl00$ctl00$ContentContainer$MainContent$MainContent$mySubmitButton" value="Register" id="mySubmitButton" class="btn btn-primary">');

     //$('#SubmitButton').hide(); *THIS WORKS*
     //$("#SubmitButton").click(); *THIS ALSO WORKS*

     $("#mySubmitButton").click(function()
     {
          //window.alert('yes!'); *THIS WORKS*
          $("#SubmitButton").click(); // *THIS DOES NOT WORK*
     });


});
5
  • Is the form getting submitted and reloading the page? Commented Jun 29, 2020 at 20:59
  • When I press the button, the page does reload. I'm not sure if the form is being submitted or not- it reloads the page even when I put no input into the forms. I'm working on making a reproducible example! It's a bit difficult for me because Powerapps Portals hides the html/css code on this page. Commented Jun 29, 2020 at 21:06
  • 2
    @gt_ Try preventing the default action: $("#mySubmitButton").click(function(e){e.preventDefault(); $("#SubmitButton").click();}); Commented Jun 29, 2020 at 21:13
  • 1
    @hev1 Wow thank you so much! Worked perfectly! How can I mark that as the answer? Commented Jun 29, 2020 at 21:22
  • @gt_ I've added an answer. Commented Jun 29, 2020 at 21:24

1 Answer 1

3

You need to prevent the default action to stop the form from submitting when the button is clicked.

$("#mySubmitButton").click(function(e){
    e.preventDefault(); 
    $("#SubmitButton").click();
});

Alternatively, you can set the button's type to "button" so clicking it does not submit the form by default.

$('#SubmitButton').after('<input type="button" name="ctl00$ctl00$ContentContainer$MainContent$MainContent$mySubmitButton" value="Register" id="mySubmitButton" class="btn btn-primary">');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.