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
How to Create a File in Memory For User to Download, But Not Through Server
Ecmascript 6 Arrow Function That Returns an Object
Detect When a Browser Receives a File Download
Generate Random String/Characters in JavaScript
How to Test For an Empty JavaScript Object
How to Work Around JavaScript'S Parseint Octal Behavior
Fastest Way to Flatten/Un-Flatten Nested JavaScript Objects
Node.Js - Syntaxerror: Unexpected Token Import
How to Create an Accurate Timer in JavaScript
Convert Utc Date Time to Local Date Time
How to Calculate Number of Days Between Two Dates
Does Es6 Introduce a Well-Defined Order of Enumeration For Object Properties
Accessing the Web Page'S Http Headers in JavaScript
What Is the Use of the JavaScript 'Bind' Method
JavaScript: Natural Sort of Alphanumerical Strings
Elements Order in a "For (… in …)" Loop