1

I'm making a website and there's a form in it... now what im trying to do is very simple but i just dont know how anymore so im asking you guys. In this form you can select whether or not you want custom text on the clothing you buy. you can select yes and no with the select statement. and next to it there's a input text where you can fill in this text. What i want to do is that when the value of the select is no or still default, the input text gets disabled and when the value is yes it gets enabled.

Screenshot of output http://prntscr.com/4ym079

My Code:

        <div class="form-group col-md-6" style="margin-bottom: 25px; padding:0;">
            <label class="col-md-12 control-label" for="producttext">Eigen text op product *</label>
            <div class="col-md-12">
                <select id="producttext" name="producttext" class="form-control" required="">
                    <option class="active" value="default" selected disabled>Eigen text op product</option>
                    <option value="nee">Nee</option>
                    <option value="Ja">Ja(+€3,50)</option>
                </select>
            </div>
        </div>
        <script>
        </script>
        <div class="form-group col-md-6" style="margin-bottom:0;">
            <label class="control-label" for="product_text">Uw eigen text*</label>
            <input id="product_text" name="product_text" placeholder="Indien Ja" class="form-control input-md" type="text">
        </div>

If you still have any questions, please ask me.

DeusGladio

2
  • You could do with jQuery. Did you try anything so far? Commented Oct 22, 2014 at 13:56
  • tried just regular Javascript but didnt work out... Commented Oct 22, 2014 at 13:57

4 Answers 4

3

Attach a onchange function and enable/disable the input:

document.getElementById("producttext").onchange = function() {
    document.getElementById("product_text").disabled = (this.value == "nee" || this.value == "default");
}
document.getElementById("producttext").change(); //to trigger on load

Or with jQuery:

$("#producttext").change(function() {
    var disabled = (this.value == "nee" || this.value == "default");
    $("#product_text").prop("disabled", disabled);
}).change(); //to trigger on load

Fiddle: http://jsfiddle.net/covq84f7/

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

2 Comments

@DeusGladio -- Mark the answer if it did what ya needed!
I'm trying haha but i have to wait 3 more minutes??
0

Using jQuery:

$('#producttext').change(function () {
    if ($(this).find('option:selected').text() != 'nee') {
        $('#product_text').prop('disabled', false);
    } else {
        $('#product_text').prop('disabled', true)
    }
});

1 Comment

this.value will be the selected options value. No need to find
0

Please write onchange javascript event on select option and try below code to get value

var e = document.getElementById("selectId");
var selectedValue = e.options[e.selectedIndex].value;

if(selectedValue == "yes")
{
//ur code
}else
{ 
//ur code
}

Comments

0

DEMO

Here is the jQuery code you need. You can listen to the change event and change the state of the input based on the value of the select. In order for this to work when the page loads, trigger the change event using, .change().

$(function() {
    $('#producttext').on('change', function() {
       $('#product_text').prop('disabled', this.value != 'Ja');
    })
    .change();
});

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.