javascript - How to reorder form fields using jquery -
i have php page form this:
<form class="form3"> <label class="form3"><input type="checkbox"> item-1</label> <label class="form3"><input type="checkbox"> item-2</label> <label class="form3"><input type="checkbox"> item-3</label> <label class="form3"><input type="checkbox"> item-4</label> <label class="form3"><input type="checkbox"> item-5</label> </form>
i have javascript variables set this:
var checked = ["", "yes", "", "yes", ""];
now when page loaded, want check if the checked value "yes" want put input items first in teh list , remaining ones later, this:
<form class="form3"> <label class="form3"><input type="checkbox" checked="checked"> item-2</label> <label class="form3"><input type="checkbox" checked="checked"> item-4</label> <label class="form3"><input type="checkbox"> item-1</label> <label class="form3"><input type="checkbox"> item-3</label> <label class="form3"><input type="checkbox"> item-5</label> </form>
i assume, jquery way it. means, need loop through form fields (which not sure how) , inside loop check if corresponding variable blank or 'yes', , if yes, print form field. again go through same loop , print fields corresponding variable blank.
hope logic correct.
i able loop through form elements this:
$.each($('.form3'),function(i,e){ if(checked[i] == "") { e.appendto($('.form3')); } });
but obviously, appendto line gives me error:
uncaught typeerror: object #<htmllabelelement> has no method 'appendto'
so, need know how pick each 1 of rows , reorder them while looping.
thanks
try instead:
$('.form3').each(function(i,e) { if(checked[i] == "") { $(this).find('input[type=checkbox]').attr('checked', true); $(this).appendto($('form')); } });
Comments
Post a Comment