6

I'm trying to implement a javascript code for the html5 form that will responsible for disabling/enabling the button until the select element is not selected. Here is the codes:

HTML:

<form name="courseForm" id="courseForm">
    <select name="coursetype" id="coursetype" onchange="enableStartBtn()">
        <option value="0" selected="selected">Select here...</option>
        <option value="1">CourseName 1</option>
        <option value="2">CourseName 2</option>
        <option value="3">CourseName 3</option>
    </select>
<button type="button" id="startBtn" disabled="disabled" onclick="Test()">Start</button>
</form>

and JavaScript:

function enableStartBtn() {
    $('#startBtn').removeAttr('disabled');
}

function Test() {
    var form = $('#courseForm');
    var course = $('#coursetype', form).val();
    if (course == '0') {
        alert('Select the course first!');
    } else if (course == '1') {
        alert('CourseName 1 is selected!');
    } else if (course == '2') {
        alert('CourseName 2 is selected!');
    } else if (course == '3') {
        alert('This course is not implemented yet');
    }
}

I'm using JQuery 1.9.1 in my code, everything looks like ok, but the when value is selected the button is not enables. Any help would be appreciated.

UPDATE: Sorry guys forgot to mention that I'm also using JQuery Mobile 1.3.0, so without including jquery mobile it works, but with jquery mobile not. Here is the jsfiddle version: http://jsfiddle.net/dozent/UVQ4m/

5
  • use .prop('disabled',false) to enable Commented May 15, 2013 at 17:31
  • 2
    It works fine for me - jsfiddle.net/JMkRa , although I do agree you should use prop for this Commented May 15, 2013 at 17:36
  • You can also add "disabled" to your <option> markup so the user can't re-select this: <option value="0" selected="selected" disabled> Commented May 15, 2013 at 17:37
  • If you're trying to toggle it being disabled (based on the selected item), you could use this: jsfiddle.net/JMkRa/2 . It uses a special data-* attribute to target not implemented courses, instead of storing that in the JS Commented May 15, 2013 at 17:50
  • @lan Please have a look to update part of my question. Commented May 15, 2013 at 20:15

3 Answers 3

6
<form name="courseForm" id="courseForm">
    <select name="coursetype" id="coursetype">
        <option value="0" disabled>Select here...</option>
        <option value="1">CourseName 1</option>
        <option value="2">CourseName 2</option>
        <option value="3">CourseName 3</option>
    </select>
    <button type="button" id="startBtn" disabled>Start</button>
</form>

And do :

var courses = ['1','2']; // available courses

$('#coursetype').one('change', function() {
     $('#startBtn').prop('disabled', false);
});

$('#startBtn').on('click', function() {
    var val = $('#coursetype').val(),
        msg = $.inArray(val, courses) != -1 ? 'CourseName '+val+' is selected!' : 'This course is not implemented yet';
    alert(msg);
});

FIDDLE

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

9 Comments

Nice. Although maybe it makes sense to just put a special data-* attribute on the "not implemented" courses. Also, why are you using .one()?
Why .one()? Wouldn't it be better to enable/disable the button according to course availability?
Once the select is changed, the button is enabled, and you can't select the first option again, so there's no need to execute the event handler more than once.
But I wouldn't want the button enabled if the course were not available. I would disable it if the user selects course 3. I suppose that's a UI issue, though.
@DerekHenderson - I would probably do something like that as well, but then you don't need a button, you'd do everything in the change event, as there's no need for a button to tell you if the course is available if the button is disabled when the course is unavailable ? Anyway, the question was to enable the button on the first change of the select, and having the event handler for more than one time is unesseccary !
|
1

use prop()

function enableStartBtn() {
   $('#startBtn').prop('disabled',false); //<----to remove  disable attr of the button
}

you need to change the property of the button here not just remove the attribute

Comments

0

include in header area

  1. not this =$('#coursetype').one('change', function() {
  2. it should= $('#coursetype').on('change', function() {

Comments

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.