X

LATEST #JQUERY NEWS

jQuery Snippets: How to Conditionally Enable Submit


One easy way to prevent users from submitting an empty form in a pinch is to conditionally disable the submit button. This might be technically be considered a best practice, but if you’re in a rush and don’t have the time to write an entire set of validation rules, this is a solution (even if it’s only a temporary one), that works.

Here’s how it works. First, you’ll need to disable the attribute in your HTML. This is actually pretty simple and straightforward, all you really need to do is add the disabled attribute (simply the word “disabled”, or, if you’re using XHTML disabled=”disabled”) to your element’s HTML. See the snippet below for an example:

<input type="submit" id="submit" value="Submit" disabled>

Then, in your jQuery, you’ll add some simple code that will conditionally remove the disabled attribute.

The jQuery code uses the .keyup() event method to verify that the user did press at least one key in the selected input field (in the case of the code snippet below, we’re selecting the #name input field — so that means that the submit button won’t be enabled until something is entered in the name field). Then, we use the .attr() method to remove the disabled attribute value from the submit field, thus enabling it and allowing our user to submit the form.

To see it in action, check out the jQuery code snippet below:

$('#name').keyup(function() {
 $('#submit').attr('disabled', !$('#name').val());
});

Like we already said, this method of “validation” (we use that term loosely) is hardly a best practice, but it’ll get the job done in a pinch. Plus, if you’re new to jQuery, just playing around with this code snippet is a good way to get a handle on both the .keyup() and .att() methods, while also getting some exposure to HTML attributes and how you can use jQuery to manipulate them.

Happy coding!

Recently Published

article image
»

7 Responsive jQuery Dropdown Menu Plugins

Dropdown Menu Plugins Dropdown menus are mostly used for website ...

article image
»

20 Stylish and Modern jQuery Tooltip Plugins

jQuery Tooltip Plugins Tooltips are a great option for showing extra ...

article image
»

15 Newest jQuery plugins for responsive website

Due to the proliferation of smartphones and tablets, responsive ...

article image
»

How to use Redux with jQuery

State management is one of most integral parts of any software and a ...

article image
»

Fade HTML Table Row on Hover Using jQuery

HTML tables are the ideal choice for displaying data in tabular form. ...

article image
»

Drag and Drop HTML Table Rows Using jQuery

HTML tables are the preferred UI option for displaying data. Sorting, ...

article image
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...