1

I have a webform, below's a section of the form (the selection field)

<select id="edit-submitted-bring-alcohol" name="submitted[bring-alcohol]" class="form-select">
    <option value="yes" selected="selected">Yes</option>
    <option value="no">No</option>
</select>

I now use jquery to dynamically setup this field.

if (age < 19) {
    $('#edit-submitted-bring-alcohol').val('no');
    $('#edit-submitted-bring-alcohol').prop('disabled', true);
}

This works great. When user entered their age, this method automatically change the selection to 'no' (if they are under 19), and disabled by itself. However, the problem I have now is that the submitted value I received is 'yes' when the selection field is disabled (apparently it chooses the default value).

So could anyone help me out?

2

You can do this by removing the 'selected' attribute first, and setting the new selected attribute, prior to disabling.

e.g:

if (age < 19) {
    $("#edit-submitted-bring-alcohol option:selected").removeAttr('selected');
    $("#edit-submitted-bring-alcohol option[value='no']").attr('selected', 'selected')
    $('#edit-submitted-bring-alcohol').val('no');
    $('#edit-submitted-bring-alcohol').prop('disabled', true);
}

Here's a jsfiddle link for tinkering: http://jsfiddle.net/AwUwt/

Edit: Alternatively, you can just hide the select box

if (age < 19) {
    $("#edit-submitted-bring-alcohol option:selected").removeAttr('selected');
    $("#edit-submitted-bring-alcohol option[value='no']").attr('selected', 'selected')
    $('#edit-submitted-bring-alcohol').val('no');
    $('#edit-submitted-bring-alcohol').css('display', 'none');
}
  • Sorry David, I tried; this doesn't work. And it seems to me that, when a webform component is disabled, the webform will not choose the default value from which the client set (in this case, my javascript) rather it will search backstage looking for the default value site-admin set (at webform management UI). – movila Sep 30 '13 at 1:47
  • @movila another option is to just hide the select box. Despite both JS methods working, that may play more nicely with webform. Answer updated. – David Thomas Sep 30 '13 at 1:49
  • yeah, seems like the only option. Thanks a lot though :) – movila Sep 30 '13 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.