Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery insertAfter only once if element exist

Tags:

jquery

How to check if an element exist , and if it does i want the insertAfter to be not valid.And if the element isn't exist, then i want the insertAfter to be valib but only once.My goal is to send only once insertAfter if an element isn't existing and if it does not send anything

like image 221
shalamhez Avatar asked May 31 '11 06:05

shalamhez


1 Answers

Just do a length check. Assuming you have a selector #sel, and you want to insert something after it.

var foo = 'something';
if ($('#sel').length) {
   //do something if elem is present
} else {
    $('#sel').insertAfter(foo);
}
like image 88
JohnP Avatar answered Sep 28 '22 04:09

JohnP