Here’s my table.. I want clean up the jquery so that I can use this logic on many rows in the table’s html.
<table>
<tr>
<td class="check"><input class="select_service3 user_checkbox" type="checkbox"></td>
<td class="name">NAME</td>
<td class="duration">
<span class="duration3">30 min</span>
<a class="custom_duration_link3">Custom</a>
<span class="custom_duration_input3"><input class="custom" type="text"> min <button class="custom_duration_save3">Save</button></span>
</td>
<td class="price">$25 <a class="custom_price_link3">Custom</a></td>
</tr>
</table>
jquery
$(".custom_duration_link3").hide();
$(".custom_price_link3").hide();
$(".custom_duration_input3").hide();
$(".select_service3").click(function(){
if ($(".select_service3").is(":checked"))
{
$(this).closest("tr").addClass("active");
$(".custom_duration_link3").show();
$(".custom_price_link3").show();
$('.custom_duration_link3').click(function(){
$('.custom_duration_link3').hide();
$('.duration3').hide();
$('.custom_duration_input3').show();
});
$('.custom_duration_save3').click(function(){
$('.custom_duration_input3').hide();
$('.duration3').show();
$('.duration3').addClass("notused");
});
}
else
{
$(this).closest("tr").removeClass("active");
$('.custom_duration_link3').hide();
$('.custom_duration_input3').hide();
$('.duration3').show();
$('.duration3').removeClass("notused");
$('.custom_price_link3').hide();
}
});
What an I do to clean up this code and not have to do “.duration1”, “.duartion2”, etc…
Thanks for help.
Basically, use the
thisobject in your function to retrieve the active row. Then only reference class names within the scope of that row. Here’s a quick demo:http://jsfiddle.net/znxQk/
The idea is to use something like this: