CF7 jquery custom text on submit

Viewed 47

I have a simple radio button on CF7

[radio radio-698 id:domanda2 use_label_element default:1 "0.Nessuna Risposta" "risposta1" "risposta2"]
[submit "Invia"]

I want to display text "Hello Word" by jquery when button submit is pressed, but this code doesn't work - can you help me, please?

jQuery(document).ready(function($){
  $('.wpcf7-submit').click(function() {
    var val1 = $('#domanda2').val();
    if (val1 == '0.Nessuna Risposta' || val1 == 'risposta1' || val1 == 'risposta2') {
      $(field1).text("Hello world!");
      return false;
    }
  });
});
1 Answers

There's a few simple issues with your jQuery. This should work for you assuming that there is an item with selector #field1.

Note, you can't get the value of an input in a radio or checkbox without specifying that it's the :checked one.

jQuery( function ($) {
    $(document).on( 'click','.wpcf7-submit', function (e) {
        let val1 = $('#domanda2 input:checked').val();
        if (val1 === '0.Nessuna Risposta' || val1 === 'risposta1' || val1 === 'risposta2') {
            $('#field1').text("Hello world!");
        }
    });
});
Related