javascript - jQuery get number of child elements with specific parameters -
i have following piece of code, being executed on submit of form. yet following logic incorrect intent develop universal function work forms in dom. after failing on , on ended creating function, specific form, verifies if values of input fields in form same custom defval attribute, if form stop submitting , error message pop up.
my question consists on following: how can check if child elements of form meet specific parameters, example being input field , have .val() == .attr('defval')?
i've tried using .find(), .children() , .childnode functionalities. please suggest me solution , if possible explain code
html:
<div class="login" align="center"> <div class="formwrapper"> <form action="index.php?r=backoffice" method="post"> <input type="text" name="username" required value="username" defval="username" maxlength="16"> <input type="password" name="password" required value="password" defval="password" maxlength="16"> <input type="reset" value="reset"> <input type="submit" value="submit"> <hr/> <a href="#" class="helplink">can't log in?</a> </form> </div> <div class="infowrapper"> <div class="info"> </div> </div> jquery:
$('div.login').ready(function(){ $('div.login form').submit(function(e){ if( $('div.login form input').val()==$('div.login form input').attr('defval') ){ var nummsgs=$('div.login form').children(); if( nummsgs.length<=6 ){ var formhtml=$('div.login form').html(); $('div.login form').html('<a class="sysmsg" href="#">error::invalid username/password</a>'+formhtml); $('div.login form a.sysmsg').addclass('errfatal').fadein(300).delay(5000).fadeout(300); } e.preventdefault(); } }); });
try (this pattern)
$(function () { var elems = $(".login input[defval]"); console.log(elems.length); $.each(elems, function (i, el) { if ($(el).val() === $(el).attr("defval")) { // stuff console.log(i, $(el).val() === $(el).attr("defval"), $(el)); }; }); });
Comments
Post a Comment