Is There an "Exists" Function For Jquery

Is there an exists function for jQuery?

In JavaScript, everything is 'truthy' or 'falsy', and for numbers 0 means false, everything else true. So you could write:

if ($(selector).length)

You don't need that >0 part.

Check if JQuery object exists in DOM

If performance is a really big concern for you, native methods are generally the way to go.

document.contains($myJQueryEl[0])

Outperforms the next fastest jQuery operation, $elem.closest('body'), by around 4 times according to this answer and its comment.

If you're really looking for performance for non-document elements, however, Shadow DOM is definitely your best option.

Check if Function Exists before Calling?

I'm assuming that you're wanting to check and make sure that lettering exists, try this:

http://api.jquery.com/jQuery.isFunction/

Here's an example:

if ( $.isFunction($.fn.lettering) ) {
$(".cs-text-cut").lettering('words');
}

Check if element exists in jQuery

$('elemId').length doesn't work for
me.

You need to put # before element id:

$('#elemId').length
---^

With vanilla JavaScript, you don't need the hash (#) e.g. document.getElementById('id_here') , however when using jQuery, you do need to put hash to target elements based on id just like CSS.

Can I call an exist javascript function into jquery function

First, add the js file to your HTML right after your jQuery

<html>
<head>
<script src="functionToUse.js"></script>
</head>
</html>

If you wish to use a function from that file in your jQuery code.

$('#buttonform').click(function (){
funcFromFile(); //function to call from other javascript file
$('#dialogform').dialog('open');
});

jQuery - how to check if an element exists?

You can use length to see if your selector matched anything.

if ($('#MyId').length) {
// do your stuff
}

jQuery determining if element exists on page

    if( $('select[name="modifier_option"]').length )
{
// it exists
}

Is there an exists function for jQuery?

In JavaScript, everything is 'truthy' or 'falsy', and for numbers 0 means false, everything else true. So you could write:

if ($(selector).length)

You don't need that >0 part.

How to check if value exist in jQuery .data() function

Here

grep (which does not do objects) did not work for me either.

Here is a $.each version

DEMO

$(function() {
$("form[name=update]").data("values", {"v1": "value11", "v2": "value2", "v3": "value3", "v4": "value4", "v5": "value5"});
$(".but").on("click",function(e) {
var fData = $("form[name=update]").data("values");
var val = $(this).val();
console.log(fData);
$.each(fData, function(n,i) {
console.log("!!!",n,i)
if (i===val) {
alert("duplicate value");
return false;
}
})
e.preventDefault()
});
});

This is now I had a look, the same method as danronmoon's solution

DEMO

$(function() {
$("form[name=update]").data("values", {"v1": "value11", "v2": "value2", "v3": "value3", "v4": "value4", "v5": "value5"});
$(".but").on("click",function(e) {
var fData = $("form[name=update]").data("values");
var val = $(this).val();
console.log(fData);
for (var o in fData) {
console.log("!!!",o,fData[o])
if (fData[o]===val) {
alert("duplicate value");
break;
}
}
e.preventDefault()
});
});


Related Topics



Leave a reply



Submit