Checking if element exists with jQuery -
i have block of code looks this:
//account number account = $(msg).find('[name=account_number]').val(); if(account){ $('#progressupdates').append('<span class="glyphicon glyphicon-ok"></span> found account number<br />'); }else{ $( '#progressupdates').append('<span class="glyphicon glyphicon-remove"></span> account number not found<br />'); }
the problem is, if element being assigned account doesn't exist, throws error. tried doing :
if(account.length){}
but issue isn't if statement, assigning variable. there better solution this?
don't call val()
account = $(msg).find('[name=account_number]');
and chack length -
if(account.length){
Comments
Post a Comment