1

I want when I check my checkbox enable input box, I made that code like this:

function free(){
    var free_var = document.getElementById('free_id');
    var delivery_price = document.getElementById('delivery_price');

    if(free_var.checked){
        delivery_price.disabled=true;
    }else{
        delivery_price.disabled=false;
    }                       
}   

but my problem is that i have more checkboxes and inputs like this:

<input type="checkbox" name="site[]" value="47.5" class="c26"/> 37.5  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c26_i" disabled/>
    <input type="checkbox" name="site[]" value="48" class="c27"/> 41  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c27_i" disabled/>
    <input type="checkbox" name="site[]" value="48.5" class="c28"/> 44  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c28_i" disabled/>
    <input type="checkbox" name="site[]" value="49" class="c29"/> 47  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c29_i" disabled/>
    <input type="checkbox" name="site[]" value="49.5" class="c30"/> 50  <input type="text" name="quantity[]" placeholder="Količina" style="width:60px;" class="c30_i" disabled/>

This is code in jsfiddle: https://jsfiddle.net/b02e392v/

Thanks

3
  • You need to call your free() function onchange of your checkbox. Also you aren't using the id's u have in your free() function... Commented Nov 18, 2015 at 18:51
  • So what is it that you want the code to do differently? Also, how are 'free_id' and 'delivery_price' relevant? They're nowhere in the HTML. Also, can you add all parts needed to demonstrate the problem, to your jsfiddle? Commented Nov 18, 2015 at 18:51
  • yes, sorry i didn't mention that my js code is from another part of my code, so id's aren't problem Commented Nov 18, 2015 at 18:53

1 Answer 1

4
$("input[type='checkbox']").change(function(){
    var status = this.checked;
    $(this).next().attr("disabled", !status);
});

On change of checkbox fetch whether it is checked or not, accordingly set the disabled attribute of the input next to the clicked checkbox.

Play it here.

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

3 Comments

Very nice solution +1
Glad it worked, consider voting it up and marking it correct answer.
Very nice and simple solution. Less lines of code returning expected results. Kudos.. @void

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.