Reputation: 591
If I know the attribute name, is there a better way, less intensive (I could have 100's os Swatches) way to write this? Something like: $("[attribute.name=='Clear-Matt']").$(this).attr('cost');
If there is not would this each function stop when (name=="Clear-Matt") or will it keep going to the end of the swatches? If so is there a way to stop it?
var swatchCost='';
$(xmldoc).find('Swatch').each(function() {
var name = $(this).attr('name');
if (name=="Clear-Matt") {
swatchCost=$(this).attr('cost');
}
});
XML:
<Swatch name="Clear" title="Clear" alt="Frame" cost="100" match=""></Swatch>
<Swatch name="Clear-Matt" title="Clear-Matt" alt="Frame" cost="11" match=""></Swatch>
<Swatch name="Blue-Orange" title="Blue-Orange" alt="Frame" cost="200" match=""></Swatch>
<Swatch name="Pink-Blue-Clear" title="Pink-Blue-Clear" alt="Frame" cost="300" match=""> </Swatch>
<Swatch name="Blue" title="Blue" alt="Frame" cost="11" match=""></Swatch>
Thanks.
Upvotes: 0
Views: 18
Reputation: 123739
You can do this way using the attribute selector.
$(xmlDoc).find('[name=Clear-Matt]').attr('cost'); // or use 'Swatch[name=Clear-Matt]'
And for your question of existing the for-each loop once you got what you need you can just do a return false;
which is equivalent to break;
statement.
if (name=="Clear-Matt") {
swatchCost=$(this).attr('cost');
return false; //break the iteration now.
}
Upvotes: 1