Is there an “exists” function for jQuery?
Posted By: gomercobs
How can I check the existence of an element in jQuery?
The current code that I have is this:
if ($(selector).length > 0) {
// Do something
}
Is there a more elegant way to approach this? Perhaps a plugin or a function?
Solution
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.
Answered By: Tim Büthe
Related Articles
- Vue v-if statement to check if variable is empty or null
- What's the difference between If not (variable) and…
- How do the PHP equality (== double equals) and…
- Specifying java version in maven - differences…
- How do Python's any and all functions work?
- What does this symbol mean in JavaScript?
- Could not calculate build plan: Plugin…
- javascript .replace and .trim not working in vuejs
- Failed to execute goal…
- How do i update a javascript variable as its value changes?
- Is it possible to apply CSS to half of a character?
- Aurelia bundling issue with virtual directory
- What does a "Cannot find symbol" or "Cannot resolve…
- Aurelia UX showcase app fails to load
- Check synchronously if file/directory exists in Node.js
- How to resolve…
- How does PHP 'foreach' actually work?
- How can I make a timer slower and slower until it stops?
- Simplest way to create Unix-like continuous pipeline…
- How to change the color of vaadin-select-text-field…
- Using any() and all() to check if a list contains…
- Grow left and right css animation
- How to check if array is empty or does not exist?
- Simple way to transpose columns and rows in SQL?
- How to format a phone number in a textfield
- Finding all possible combinations of numbers to…
- Pandas-way to separate a DataFrame based on…
- C compile error: Id returned 1 exit status
- Baffling variadic templates exercise
- Svelte application not working on android version…
- How to use nanosleep() in C? What are `tim.tv_sec`…
- @selector() in Swift?
- For-each over an array in JavaScript
- m2eclipse error
- Building executable jar with maven?
- How can I check if an element exists in the visible DOM?
- python 3.2 UnicodeEncodeError: 'charmap' codec can't…
- Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
- Worker Script Failing to Load for Vue Webpack Built App
- Include resource directory into Quarkus target output
- How to save array or matrix in Julia with specific format?
- Easy interview question got harder: given numbers…
- "Thinking in AngularJS" if I have a jQuery background?
- Ukkonen's suffix tree algorithm in plain English
- Why am I getting a "401 Unauthorized" error in Maven?
- ./components/Avatar.tsx Error: Cannot find module…
- Lit-Element binding to svg image. TypeError: Cannot…
- Plugin…
- Webpack: Unable to find module with ID: main --…
- boolean in an if statement
- Combine two rows in Pandas depending on values in a…
- How to set HTML5 required attribute in Javascript?
- "[BABEL] .value is not a valid Plugin property"…
- JavaScript gives NaN error on the page but variable…
- Getting visitors country from their IP
- How can I declare optional function parameters in…
- jQuery Mobile: document ready vs. page events
- How to use Servlets and Ajax?
- Convert array to nested JSON object - Angular Material tree
- Java SSLHandshakeException "no cipher suites in common"
- Show/hide 'div' using JavaScript
- Sort table rows In Bootstrap
- How to do a GitHub pull request
- Matching identical columns
- Problems using Maven and SSL behind proxy
- Maven is not working in Java 8 when Javadoc tags are…
- How do you go into production with polymer project?
- Incorporate JQuery-file-upload to application
- Maven: The packaging for this project did not assign…
- What does AngularJS do better than jQuery?
- TypeScript metadata reflection references other…
- Svelte for IE11 with Babel transpiler on Snowpack
- MongoDB - Pivot data down instead of flattening with…
- PHP/JavaScript Redirect Issue
- Fastest way to list all primes below N
- Am I paranoid? "Brutally" big Polymer website after…
- Regular expression to match numbers with or without…
- How to use html template with vue.js
- Inserting VueJS component template in HTML page not working
- How can I manually compile a svelte component down…
- JavaScript TypeError: Cannot read property 'style' of null
- How to split and modify a string in NodeJS?
- CSS selector for first element with class
- Highlight a word with jQuery
- Could not find…
- Is if(document.getElementById('something')!=null)…
- Why does C++ code for testing the Collatz conjecture…
- What is your most productive shortcut with Vim?
- constraints reverting back to original position when…
- What is the difference between ng-if and ng-show/ng-hide
- Why doesnt my table sort my div variable in numerical order?
- Search code inside a Github project
- How to stop a JavaScript for loop?
- webpack-dev-server npm run dev throwing TypeError:…
- Reference - What does this regex mean?
- How to detect if multiple keys are pressed at once…
- The definitive guide to form-based website authentication
- Responsive width Facebook Page Plugin
- Javascript "Cannot read property 'length' of…
- Jquery If radio button is checked
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.