Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one element is exist under a form in jQuery

I have a form with id commentform and if any logged in user visit the page a p tag gets generated under the form that with class logged-in-as. Now I am trying to check if that p exists and if not exists then do my validation which uses keyup(). Here is a small snippet...

$('form#commentform').keyup(function() {
        if( ! $(this).has('p').hasClass('logged-in-as') ) {
            ....
            } else {
                ......
            }
        }
    });

Now the problem is that the if( ! $(this).has('p').hasClass('logged-in-as') ) is not returning me the expected result whether or not that specific p exists.

Can any of you guys tell me any other/better way to check this?

like image 292
iSaumya Avatar asked Dec 18 '22 22:12

iSaumya


1 Answers

$('form#commentform').keyup(function() {
    if($(this).find('p.logged-in-as').length == 1) {
        ....
        } else {
            ......
        }
    }
});

You can do this to find it.

like image 172
AtheistP3ace Avatar answered Feb 09 '23 00:02

AtheistP3ace