0

I need to 'click' one button using JavaScript, when the user clicks another button.

In Chrome on Mac, the following works fine. However, in Safari on Mac, clicking the button simply reloads the current page, rather than triggering the expected behaviour.

JS

$( 'document' ).ready( function() {
    var $myForm = $('#my-form');
    $('#button-fake').on('click', function(){
        console.log("FAKE CLICKED");
        if ($myForm[0].checkValidity()){
            console.log("FORM VALID");
            $('#button-real').click();
            console.log("FORM SUBMITTED");
        }
    });              
});

HTML

<input type="submit" id="button-fake" value="Fake button" />

<input type="submit" id="button-real" value="Real button" />

PS: Not my idea, I just need to get it working.

2
  • Why can't you extrapolate the code to a common function and call that? Commented Mar 1, 2019 at 9:57
  • Whatever button-real runs is a blackbox. Commented Mar 1, 2019 at 9:59

2 Answers 2

2

You may need to add e.preventDefault();

var $myForm = $('#my-form');
$('#button-fake').on('click', function(e) {
  e.preventDefault();
  console.log("FAKE CLICKED");
  if ($myForm[0].checkValidity()) {
    console.log("FORM VALID");
    $('#button-real').click();
    console.log("FORM SUBMITTED");
  }
});

$('#button-real').on('click', function(e) {
  console.log('button-real clicked')

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id='my-form'>
  <input type='text' required>
  <button type='submit' id='button-fake'>Submit</button>
  <button type='button' id='button-real'>Real</button>

</form>

Sign up to request clarification or add additional context in comments.

1 Comment

Just realised: If I add e.preventDefault(), it no longer triggers the HTML 5 form validation messages. So that's not an option
1

The button will trigger a form submit. The form submit will reload the page. To prevent this, call preventDefault().

$( 'document' ).ready( function() {
        var $myForm = $('#my-form');
        $('#button-fake').on('click', function(e){
                e.preventDefault(); // <- This
                console.log("FAKE CLICKED");
                if ($myForm[0].checkValidity()){
                        console.log("FORM VALID");
                        $('#button-real').click();
                        console.log("FORM SUBMITTED");
                }
        });              
});

3 Comments

Thank you, I completely missed that! PS: Same answer as @brk, but more explanatory detail, therefore I am accepting this solution.
Just realised: If I add e.preventDefault(), it no longer triggers the HTML 5 form validation messages. So that's not an option.
I don't feel there is a way to actually circumvent that... You could try replace the HTML5 validation with a javascript based one

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.