Change default submit button on Detail page with two forms

Change default submit button on Detail page with two forms

  
I have a Detail page for two entities linked by a OneToOne. The entities are Question and Answer. After saving the Question, an AJAX refresh shows the AnswerForm. Both entities only have an ID and a text field. When a user presses Enter in the text field for the AnswerForm, the Save action on my QuestionForm runs. I want to change this behaviour so that when a user presses enter in the QuestionForm, it uses this submit, and submits the Answer when in the AnswerForm text field. 

Is it possible to change the default button in an AJAX refresh, or should I write some JavaScript that checks the focused field and uses the right submit based on context? 

Kindest regards,
Robrecht
Both. 

1 - Put the default button script** into a IF condition with a flag. On AJAX refresh you can turn this flag on, so the script will be rendered on browser and executed on refresh. 

2 - It's exactly on the way you have explained above.

** 
$(document).ready(function ()
{
    $(".tbPassword").keydown(function (e)           /* or keyup  */
    {
        if (e.keyCode == 13) // 27=esc
        {
            $("form").submit();
        }
    });
});
Awesome, that works. Thanks for the help! :) 
Turns out it didn't work, or at least not in the way I expected it. the JavaScript works, because I get a message logged in my console, but pressing enter while in the AnswerForm TextField still submits the QuestionForm. My current JavaScript is below. I'm sure that I'm referencing the correct IDs everywhere. Using $('#AnswerForm').submit() doesn't work either - it still submits the QuestionForm. 

"<script type='text/javascript'>
$(document).ready(function ()
{ $('#" + btnAntwoordToevoegen.Id + "').addClass('Is_Default');
  $('#" + btnVraagOpslaan.Id + "').removeClass('Is_Default');

    $('#" + Antwoord_Antwoord.Id + "').keydown(function (e)           /* or keyup  */
    {
        if (e.keyCode == 13) // 27=esc
        {
            console.log('yupyup');
            $('#" + btnAntwoordToevoegen.Id + "').click();
        }
    });
});</script>"
Are you still using the Default Button property set to true? Since now you are handling this behavior by javascript, you must take off this. 
No, I checked that as well but I had set it to False on both buttons. 
Humm it's weird... so how the button click has being triggered? I dont understand. Anyways, try to put some return false; in the end of function to avoid udesireble javascript execution.
I added e.preventDefault() and e.stopPropagation() to cancel other javascript executions. I've replaced it with return false now though :) Right now the button doesn't get clicked (not in QuestionForm nor in AnswerForm), but if I enter while in my AnswerForm TextField I get the yupyup in my console so it should work. Weird. 
I deleted and rewrote the JS (it's still the same code, though) and now it does work. Odd, but still. Thanks again, André! 
Could you post the finished script or better yet a simple OML file that shows this?  I have a similar need.

Thanks,
Curt
Sure thing, check the attachment. It's a simple example, you could also use a listener function in JavaScript that takes a Form's RuntimeId as parameter and then listens to all the inputs or the last input of the form, or something like that. Good luck with it :) 
I just noticed that the above OML doesn't append the Is_Default CSS to the second button, nor does it remove the Is_Default class from the first button, which is kind of odd - it does do that in my actual code. Let me know if you want me to look into it, I just tried fixing it for about fifteen minutes but only made it worse haha. 

Kindest regards,
Robrecht

Edit: 

I fixed it - this implementation is closer to what I actually have (in my eSpace the navigate is to an externalUrl due to separation of UI and the BackEnd).