Jquery – How to check if an element contains another element in jQuery

containsjqueryjquery-validatevalidationxval

I am using xVal combind with the jquery.validate plugin, but have come across a slight problem in that it is posting validation messages twice in a certain instance. I think this bug should be fairly easily fixed with some clever jQuery in the placement of the error message.

I am trying to find a way to see if a ul already contains an li with some text in it.

errorPlacement: function(error, element) {
     $("ul.errors:not(:contains(" + error + "))").append(error);
}

I thought something like the above may work, but not such luck. error.toString() does not work either.

Any help would be greatly appreciated.

Best Answer

Give a look to the :has selector:

$("ul:has(li:contains('" + error + "'))")

The above selector will select the UL elements which have a LI element that contains the error text.