I currently have 2 mirror sections within my DOM, one for delivery
and another for pickup
. Both of these sections contain identical content structures. Here's an example:
<div class="main-section">
<div class="description-content">
<p><span class="to-emphasize"></span></p>
</div>
<div class="field-content">
<input type="text" name="address"></input>
</div>
<div class="table-content">
<table>
<thead>
<tr>
<td></td>
</tr>
</thead>
</table>
</div>
</div>
The question I have is regarding the efficiency of jQuery targeting and retargeting in situations like this. Which strategy would be more efficient? Here are two options to consider:
Strategy A: Add delivery/pickup selector to parent and query on parent.
<div class="main-section" data-type="delivery">
<div class="description-content">
<p><span class="to-emphasize"></span></p>
</div>
<div class="field-content">
<input type="text" name="address"></input>
</div>
<div class="table-content">
<table>
<thead>
<tr>
<td></td>
</tr>
</thead>
</table>
</div>
</div>
// jQuery code example
$(".main-section[data-type='delivery'] input[name='address']").val("new value")
$(".main-section[data-type='delivery'] .to-emphasize").empty()
$(".main-section[data-type='delivery'] .to-emphasize").append("new value")
delivery_or_pickup = $(this).parents(".main-section").data("type")
$(".main-section[data-type='"+delivery_or_pickup+"']").hide()
Strategy B: Add delivery/pickup selector to all children that need querying.
<div class="main-section" data-type="delivery">
<div class="description-content" data-type="delivery">
<p><span class="to-emphasize" data-type="delivery"></span></p>
</div>
<div class="field-content" data-type="delivery">
<input type="text" name="address" data-type="delivery"></input>
</div>
<div class="table-content" data-type="delivery">
<table>
<thead>
<tr>
<td></td>
</tr>
</thead>
</table>
</div>
</div>
// jQuery code example
$("input[name='address'][data-type='delivery']").val("new value")
$(".to-emphasize[data-type='delivery']").empty()
$(".to-emphasize[data-type='delivery']").append("new value")
delivery_or_pickup = $(this).data("type")
$(".main-section[data-type='"+delivery_or_pickup+"']").hide()