0

I need to change the submit button value (text) based on the selection made in a certain register form field. I accomplished this by using jQuery:

(function ($) {
$(function() {
$("#edit-field-registrierung-als-und").change(function() {
if ( this.value == "2") {
$("#registersubmit").val("Kostenpflichtig registrieren");
}
else {
$("#registersubmit").val("Jetzt registrieren");
}
});
});
}(jQuery));

The submit button value changes as expected. But the form is not sent anymore after submitting. The page refreshes and the entered data is still in there. I don't get any error in the console or error log. I think drupal expects the very term and is 'confused' to get a different one. Does somebody know how to resolve this?

This is my register page: https://my-business-location.com/registrierung The first field on the page influences the submit button value (option 2).

  • That’s correct, the action to take on submit is determined by the submit button value. You’ll have to change it with Drupal’s AJAX API, or the backend won’t know how to process the changes you’ve made – Clive Aug 1 '18 at 7:20
  • Ok. Can you explain how this is done with Ajax API? I am no professional coder but try my best. Does it mean that I need to write Ajax code to change the submit button value? Where does this code go, custom module? – ThuleNB Aug 1 '18 at 8:57
1

Based on what Clive said and your response regarding the Ajax stuff, I edited your code. I am assuming you only want to change the text of the button for display purposes.

(function ($) {
  $(function() {

    // Create a copy of the button
    $('#registersubmit').after('<input id="new-submit" class="form-submit" value="Jetzt registrieren">');
    // Hide original
    $('#registersubmit').hide();
    // When copy  is clicked, "click" the original
    $('#new-submit').click(function(){
      $('#registersubmit').click();
    });
    // Your code with changed IDs
    $("#edit-field-registrierung-als-und").change(function() {
      if ( this.value == "2") {
        $("#new-submit").val("Kostenpflichtig registrieren");
      }
      else {
        $("#new-submit").val("Jetzt registrieren");
      }
    });
  });
}(jQuery));

Can't test it as you appear to have the code active right now, but it should work fine in this scenario.

  • Awesome, this is exactely what I needed! Many thanks! – ThuleNB Aug 1 '18 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.