javascript - Iterating over parent elements -
i have html looks (sample 1):
<tr id="q_19030" class="q_grid " style="background: none repeat scroll 0% 0% yellow;"> <a class="reset-button" data-question-id="g_1363"></a> </tr>
i have html looks (sample 2):
<fieldset id="q_19044" class="q_default " style="background: none repeat scroll 0% 0% yellow;"> <a class="reset-button" data-question-id="q_19044"></a> </fieldset>
i have jquery code this:
$(document).ready(function() { $('.surveyor_radio').parent().parent().append('<a class="reset-button">reset</a>'); $('.reset-button').click(function () { $('#'+$(this).data('question-id') + ' input[type="radio"]:checked').prop('checked', false).trigger('change'); }); $('fieldset').each(function () { var qid = $(this).attr('id'); $('.reset-button', $(this)).attr('data-question-id', qid); }); });
the jquery code has 3 functions:
one adds reset button radio questions, unchecks questions once reset button clicked, while third assigns appropriate id number data-question-id
attribute of reset button.
the problem
the code in sample 2 correct data-question-id
matches id of parent, fieldset id
.
the fieldset, however, not contain correct data-question-id
indicated in sample 1, id of particular row.
one assumption can relied upon correct id parent of reset button.
the question
how appropriately iterate on each reset buttons parent, id, set data-question-id
of reset button.
my best attempt $(".reset-button").parent().each().attr("id");
iterate on .reset-button
elements, , set each data-question-id
parents id :
$(".reset-button").each(function() { $(this).data('question-id', $(this).parent().attr('id')); });
note not change data attribute when viewing in dom inspector, jquery stores internally, change value thats later retrieved data();
you :
$(".reset-button").attr('data-question-id', function(i, val) { return this.parentnode.id || val; });
Comments
Post a Comment