jquery - check if parentNode is undefined -


i'm getting typeerror parentnode undefined. how can check if parentnode undefined?

here method:

    function updateimages(myrow) {          var rowinputs =  j$(myrow).find('input[type="text"]');         var contact = (j$(rowinputs[0]).val());          var user = (j$(rowinputs[1]).val());         var account = (j$(rowinputs[2]).val());          if (contact !== '') {             j$(rowinputs[0].parentnode).find('img').show();             j$(rowinputs[1].parentnode).find('img').hide();             j$(rowinputs[2].parentnode).find('img').hide();         }             else if (user !== '') {             console.log('user not blank');             console.log(j$(rowinputs[1]));             console.log(j$(rowinputs[1].parentnode));             j$(rowinputs[0].parentnode).find('img').hide();             j$(rowinputs[1].parentnode).find('img').show();             j$(rowinputs[2].parentnode).find('img').hide();         }         else if (account !== '') {             j$(rowinputs[0].parentnode).find('img').hide();             j$(rowinputs[1].parentnode).find('img').hide();             j$(rowinputs[2].parentnode).find('img').show();         }         if (account !== '' && contact !== '') {             j$(rowinputs[0].parentnode).find('img').show();             j$(rowinputs[1].parentnode).find('img').hide();             j$(rowinputs[2].parentnode).find('img').hide();         }     } </script> 

i need check if parentnode rowinputs[1].parentnode undefined , if parentnode rowinputs[2].parentnode undefined.

thanks help. regards.

this suggestion:

function updateimages(myrow) {      var rowinputs =  j$(myrow).find('input[type="text"]');     var contact = rowinputs.eq(0);      var user = rowinputs.eq(1);     var account = rowinputs.eq(2);      var cont = contact.parent().find('img');     var usr = user.parent().find('img');     var aco = account.parent().find('img');      if (contact.val() !== '') {         cont.show();         usr.hide();         aco.hide();     }         else if (user.val() !== '') {         console.log('user not blank');         console.log(user);         console.log(user.parent());         cont.hide();         usr.show();         aco.hide();     }     else if (acontount.val() !== '') {         cont.hide();         usr.hide();         aco.show();     }     /* redundant same first if     if (acontount.val() !== '' && contact.val() !== '') {         j$(rowinputs[0]).parent().find('img').show();         j$(rowinputs[1]).parent().find('img').hide();         j$(rowinputs[2]).parent().find('img').hide();     }     */ } 

you code assumes there 3 elements per row , throw error because of that. if use .eq() instead jquery object , no undefined error. if post html maybe can find better way optimize code.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -