You are viewing limited content. For full access, please sign in.

Question

Question

How do I disable the submit button when the page loads?

asked on December 14, 2015 Show version history

I would like to have the submit button disabled to start. I tried this:

$(document).ready(function() {
    $('.Submit').attr("disabled", "disabled");

 

but it isn't working..

 

Thanks,

David

0 0

Answer

SELECTED ANSWER
replied on December 14, 2015 Show version history

This works:

$(document).ready(function () {

  $(document).on('change','.JobNumber input, .confirm input', function() {
    	if (!this.value) return;
    	if ($('.JobNumber input').val() == $('.confirm input').val()) {
           $('.Submit').prop('disabled',false);
        } else {
           $('.Submit').prop('disabled',true);
        }
  }).find('input.Submit').prop('disabled',true);
  
});

The reason the previous code did not work is cause for some reason Forms runs the change event on the fields on first load, so the logic says if the fields equal each other (which "" does equal "") then enable the Submit button. No explanation as to why forms runs the change event on load of the form.

1 0

Replies

replied on December 14, 2015

You are close. You want to use .prop() instead:

$('.Submit').prop('disabled',true);

 

2 0
replied on December 14, 2015

No, using attr() is fine in this case. His code actually works if you try it out on a form.

The problem is that he's missing the closing brackets. It should be:

$(document).ready(function() {
    $('.Submit').attr("disabled", "disabled");
});
1 0
replied on December 14, 2015

Good catch Ege! Didn't see that.

1 0
replied on December 14, 2015

Awesome! This worked! Would you be able to help with something else now? I need the submit button to be re-enabled if something else happens.

My original code was as follows:

$(document).ready(function () {

  $('.JobNumber, .confirm').on('change input', function () {
        
        if ($('.JobNumber input').val() != $('.confirm input').val()) {
            $('.Submit').attr("disabled", "disabled");                   
        }
        else            
        $('.Submit').removeAttr('disabled');
 
    })
})

This makes the submit button disabled if the JobNumber was incorrect, and did not match with the "confirm" field. This would only disable if the user entered something in the field, otherwise they could submit by just leaving it blank. I've decided to make the submit button default to disabled when the webpage loads, unless these two values match. How would I go about altering the above code to make this work?

 

Thanks again for your help!

0 0
replied on December 14, 2015 Show version history

Try this:

$(document).ready(function () {

  //disable the submit button when page loads
  $('.Submit').attr('disabled', 'disabled');

  //validate inputs every time they change, and enable submit if validation passes
  $('.JobNumber, .confirm').on('change', 'input', function () {
        if ($('.JobNumber input').val() == $('.confirm input').val()) {
           $('.Submit').removeAttr('disabled');                  
        } else {
           $('.Submit').attr('disabled', 'disabled');
        }
    });
});

 

0 0
replied on December 14, 2015

So I actually tried this, but it doesn't seem to be working for me. Not sure why.

0 0
replied on December 14, 2015

Try changing line 7 to:

$('.JobNumber input, .confirm input').on('change', function () {

This is assuming you have JobNumber and confirm classes added to the fields in the form designer.

1 0
replied on December 14, 2015

The submit button is not disabled when the webpage opens.

0 0
replied on December 14, 2015

Running into a meeting now, but let me test it this afternoon and get back to you.

1 0
replied on December 14, 2015 Show version history

Sounds great.

So I figured something out I think... I changed "change" to "blur" and changed line 7 back to how it was:

 

$(document).ready(function () {
 
    //disable the submit button when page loads
      $('.Submit').attr('disabled', 'disabled');
 
      //validate inputs every time they change, and enable submit if validation passes
    $('.JobNumber, .confirm').on('blur', 'input', function () {
        if ($('.JobNumber').val() == $('.confirm').val()) {
              $('.Submit').removeAttr('disabled');
        }
    });
});

 

This disables the submit button when the webpage loads. The problem is that the submit button is enabled whenever any value is entered into the JobNumber... So it's not checking to see if it matches .confirm.

0 0
replied on December 14, 2015

Can you show us the code you have in the "Javascript" section under the "CSS and Javascript" tab?

1 0
replied on December 14, 2015 Show version history
$(document).ready(function () {
 
	//disable the submit button when page loads
  	$('.Submit').attr('disabled', 'disabled');
 
  	//validate inputs every time they change, and enable submit if validation passes
	$('.JobNumber, .confirm').on('blur', 'input', function () {
		if ($('.JobNumber').val() == $('.confirm').val()) {
      		$('.Submit').removeAttr('disabled');
    	}
	});
});

I've tried the code above, and the code below:

$(document).ready(function () {
 
  //disable the submit button when page loads
  $('.Submit').attr('disabled', 'disabled');
 
  //validate inputs every time they change, and enable submit if validation passes
  $('.JobNumber, .confirm').on('change', 'input', function () {
        if ($('.JobNumber input').val() == $('.confirm input').val()) {
           $('.Submit').removeAttr('disabled');                  
        } 
    });
});

Changing "change" to "blur" on line 7 disabled the submit button when the page loaded, but then re-enabled it as soon as anything was entered into JobNumber.

0 0
replied on December 14, 2015

Ah, sorry. I see the issue now. I wasn't thinking straight.

This worked for me:

$(document).ready(function() {
  $('.Submit').attr("disabled", "disabled");
  
  $('.JobNumber').on('change', 'input', function() {
    if ($(this).val() == $('.confirm input').val()) {
      $('.Submit').removeAttr('disabled');
    } else {
      $('.Submit').attr("disabled", "disabled");
    }
  });
  
  $('.confirm').on('change', 'input', function() {
    if ($(this).val() == $('.JobNumber input').val()) {
      $('.Submit').removeAttr('disabled');
    } else {
      $('.Submit').attr("disabled", "disabled");
    }
  });
});

 

0 0
SELECTED ANSWER
replied on December 14, 2015 Show version history

This works:

$(document).ready(function () {

  $(document).on('change','.JobNumber input, .confirm input', function() {
    	if (!this.value) return;
    	if ($('.JobNumber input').val() == $('.confirm input').val()) {
           $('.Submit').prop('disabled',false);
        } else {
           $('.Submit').prop('disabled',true);
        }
  }).find('input.Submit').prop('disabled',true);
  
});

The reason the previous code did not work is cause for some reason Forms runs the change event on the fields on first load, so the logic says if the fields equal each other (which "" does equal "") then enable the Submit button. No explanation as to why forms runs the change event on load of the form.

1 0
replied on December 14, 2015

Chris,

Thanks for the help! This worked! I made one change, however :

$(document).ready(function () {

  $(document).on('change input','.JobNumber input, .confirm input', function() {
    	if (!this.value) return;
    	if ($('.JobNumber input').val() == $('.confirm input').val()) {
           $('.Submit').prop('disabled',false);
        } else {
           $('.Submit').prop('disabled',true);
        }
  }).find('input.Submit').prop('disabled',true);
  
});

On line 3 I added "input" after "change". This made it so the user can't enter a valid JobNumber, then delete it and enter something else -then immediately submit. It seems that before, it was comparing JobNumber to confirm when the user left the JobNumber field, so as long as it was the last thing they changed before submitting, they could still submit. This makes it so that as soon as the JobNumber is edited, the submit button is disabled again.

 

Thanks again so much! I really appreciate it.

0 0
replied on October 8, 2021

can anyone explain what line 10 is doing?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.