I have a simple question about the expression in jquery to select a class which involved a for loop. Basically, I have two steps:
1. Generate some html tables with a loop.
2. Use jQuery to unhide them.
Below is the simplified code to
1. generate html tables (I will add a loop index to separate each loop created tables) :
2. the key part of the second step is to unhide generated tables. My selector is written as: method_options_1'+i+', where i is a for loop index.
for (var i = 2; i <= 5; i++) {
<tr class="method_options_1'+i+'" style="display: none;"><th><label for="id_CAM_1">Chemical application Method (CAM):</label></th>
<td><select name="CAM_1_'+i+'" id="id_1_'+i+'">
<option value="2">2-Interception based on crop canopy</option>
<option value="9">9-Linear foliar based on crop canop</option></select>
</td></tr>
$('.method_options_1'+i).closest('tr').show();
}
However, this selector does not work, which means I could not show the hidden element. While, if I remove the index i from both the HTML and jquery, the selector works. Since I have to keep the i index, can anyone give me some suggestions (I tried to remove closest(‘tr’), but it does not work)?
'.method_options_1'+iis already atr, so first try removing theclosest('tr')step:See http://jsfiddle.net/WJDCm/