javascript - Get attr from each parent and set for each child -
i have datepicker jquery-ui , need set id each day (a class .ui-state-default) in format: year-month-day.
this solution, want make working every day. this:
id="2015-2-1234567891011121314151617181920212223242526272829303112345678910111213141516171819202122232425262728293012345678910111213141516171819202122232425262728293031"
var year = $(".ui-state-default").parent('td').data('year'); var month = $(".ui-state-default").parent('td').data('month'); var day = $(".ui-state-default").parent('td').children().text(); $(".ui-state-default").attr("id", "" + year + '-' + month + '-' + day + "");
and sample part of datepicker code.
<tr> <td class=" " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">2</a> </td> <td class=" " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">3</a> </td> <td class=" " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">4</a> </td> <td class=" " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">5</a> </td> <td class=" " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">6</a> </td> <td class=" ui-datepicker-week-end " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">7</a> </td> <td class=" ui-datepicker-week-end " data-handler="selectday" data-event="click" data-month="2" data-year="2015"> <a class="ui-state-default" href="#">8</a> </td> </tr>
i suggest iterating on .ui-state-default elements via jquery each performing same code within it. https://api.jquery.com/jquery.each/
$('.ui-state-default').each(function() { var parentdata = $(this).parent().data(); $(this).attr("id", "" + parentdata.year + '-' + parentdata.month + '-' + this.text + ""); });
Comments
Post a Comment