javascript - Fetch data-value of specific elements -
when creating scheduler type system allows user choose months, days, hours , minutes wish task run, have created divs represent each option. used "data-value" store actual integer value representation.
when submit form, need collect information place arrays , pass ajax. struggling figure out how so. need months class "selected", hours class "selected" , on. can shed light on how so?
i imagine there way check each element within div container not sure how so.
var monthscontainer = $("#schedmonthscontainer"); var dayscontainer = $("#scheddayscontainer"); (i = 0; < monthsofyear.length; i++) { var month = monthsofyear[i]; var monthel = $("<div>", { 'class': "timesegment selected", 'data-value': i, text: month }); monthscontainer.append(monthel); } (i = 0; < daysofweek.length; i++) { var day = daysofweek[i]; var dayel = $("<div>", { 'class': "timesegment selected", 'data-value': i, text: day }); dayscontainer.append(dayel); } html
<div class="twelvesegmentcontainer"> <div id="schedmonthscontainer"></div> </div> <div class="sevensegmentcontainer"> <div id="scheddayscontainer"></div> </div>
the following find elements have both timesegment class selected class:
$('.timesegment.selected') so, using above can retrieve values of data-value attributes this:
$('.timesegment.selected').each(function(index) { console.log($(this).data('value')); }); update
finding out parent belongs $(this) can done using: parent() or use find() on element.
option 1: using parent():
$('.timesegment.selected').each(function(index) { var parent = $(this).parent(); console.log(parent.attr('id')); console.log($(this).data('value')); }); the above retrieve id of parent element, schedmonthscontainer or 1 of other containers.
option 2: using find() on specific element:
$('#schedmonthscontainer').find('.timesegment.selected').each(function(index) { console.log($(this).data('value')); }); the above retrieve elements both timesegment , selected classes within months container.
Comments
Post a Comment