I need to write a generic jQuery method enable/disable a drop down list based on a checkbox value.
I have 2 components (each has a checkbox and a drop down list) and I don’t want to write the exact same method twice, only with different id’s of the checkbox and the drop down list.
I thought maybe to use the class selector to find the checkboxes (that will have the same class) and somehow go to their parent and search for a drop down list child of that parent (and of course make sure that both of the checkbox and dropdown list will be siblings under the same parent)
2
Answers
Here you can find a working example of a generic jQuery method that uses classes like you asked:
You’re looking for DOM traversal. If you want to be very generic, technically you don’t need
classes
orids
, you can just use you elements themself.