2

I have array of select box and array of text area with same id and class.

  <div class="reasons">
       <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>

        <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>

        <select id="reason" class="reason_select" name="reason[]">
           <option>Please Select</option>
           <option value="misc">Misc</option>
        </select>
        <textarea id="reason_comment" name="reason_comment[]" disabled="disabled/>
  </div>

In these three pairs, on change of any of the select box I need to enable there corresponding textarea.

$(document).on("change", ".reason_select", function(){
    //not getting where to start and how to identify corresponding textarea.
});
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
  • 1
    I see multiple `id="reason_comment"` (like reason). It's not correct, an *id* is unique ! – Aure77 Jun 03 '15 at 15:34
  • @Aure77 is right. this is the starting point of the way to go. most of the answers so far below do a next, this is bad as it ties the UI to a specific order. do selections either on id, class, or data attribute. IMHO any sequential selector should be avoided unless necessary or obvious like li in ul / options within a select, definitely not good in this case. – hubson bropa Jun 03 '15 at 15:38
  • Totally agree multiple elements with same id is damm bad idea..will try to assign unique id to all elements – Pardeep Dhingra Jun 03 '15 at 15:40
  • Thanks all of you for valuable answers.. – Pardeep Dhingra Jun 03 '15 at 15:40

4 Answers4

1

try

$(document).on("change", ".reason_select", function(){
    $(this).next().prop("disabled",false)
});
Balachandran
  • 9,567
  • 1
  • 16
  • 26
1

not getting where to start and how to identify corresponding textarea.

The first place to start would be to not use the same id for multiple elements. See Why is it a bad thing to have multiple HTML elements with the same id attribute?

You can use next() instead

$(document).on("change", ".reason_select", function(){
    $(this).next('textarea').prop('disabled',false);
});
Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
1

Identifiers in HTML must be unique A good read Why is it a bad thing to have multiple HTML elements with the same id attribute?

You can use $.fn.next() to traverse to immediate sibling.

Use

$(document).on("change", ".reason_select", function(){
    $(this).next('textarea').prop('disabled', false)
});
Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

You need to use unique IDs. Instead make it a class in your textboxes:

class="reason_comment"
^^^^^

And in jQuery:

 $(document).on("change", ".reason_select", function(){
  $(this).next('.reason_comment').prop('disabled',false);
 });
Zee
  • 8,420
  • 5
  • 36
  • 58