Good time of day!
There is a form, with 1 checkbox, 1 label, 1 input [text] in this order, and all by 3 (the text is hidden and depends on whether the checkbox is selected or not).
With the help of .each through, but I can’t choose the right input, either all of them or the one that is .eq() via .eq() ;
On codepen.io HTML form and script. Please help me figure it out.

 jQuery(function(){ jQuery('form :checkbox').click(function(){ jQuery(this).each(function(index, element){ if (jQuery(this).prop('checked')){ jQuery('input:text').show() } else { jQuery('input:text').hide() } }); }); }); 

https://codepen.io/masteine/pen/ZyZBqP

  • link codepen leads to an updated script that now does not demonstrate a problem - Grundy

1 answer 1

In the onClick event, add the search functionality to the input element corresponding to the checkbox processed:

 // Определяем ID чекбокса: var cbId = $(this).attr("id").replace("_check",""); // Находим соответствующий input var input = $("#" +cbId +"_input"); 

Corrected Code

However, I would recommend to wrap the blocks in divs, then the search for the corresponding input can be made more elegant:

  var input = $(this).closest("div").find("input:text"); 

Example