InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

jQuery tutorials

    jQuery Ajax

    • jQuery JSONP
    • jQuery .post()
    • jQuery .load()
    • load script with cache enabled
    • jQuery .ajax() file upload

    jQuery CSS

    • change table cells color based on value
    • check if element is visible in viewport
    • display auto fade out message
    • fitvids.js for automatically resizing videos
    • get class list of an element
    • jQuery toggleClass() examples
    • make a div stick at top on scroll
    • table with alternate color rows

    jQuery Events

    • Javascript/jQuery - disable right click
    • jQuery - text input field - change keyup and paste events
    • trigger click using jQuery or Javascript

    jQuery DOM

    • jQuery html() vs text()
    • Check if an element exists
    • find total DOM elements
    • jQuery encode string to html entities
    • jQuery - read and modify iframe content
    • jQuery - create iframe with content
    • jQuery - find select element selectedIndex, value and text
    • jQuery - get checkbox value and checked state
    • jQuery - get javascript object

    jQuery Cookbook

    • get jQuery version
    • jQuery - add text to existing div
    • jQuery sliding effect - slideUp, slideDown, slideToggle

    jQuery UI

    • jQuery UI make element movable
    • jQuery ui slider and input text box - one way binding
    • jQuery ui slider and input text box - two way binding
     
    • Home
    • > Tutorials
    • > Javascript
    • > jQuery

    jQuery – check if an element exists

    By admin on Nov 30, 2015

    jQuery when used with selectors like element tag, class, id can potentially return a jQuery object with no element in it. Since jQuery contains number of elements present in DOM using the specified selector, we can use the length property to check if an element exists.

    if ($('.someclassname').length > 0) {
      // element exists
    }
    

    jQuery Object for non-existing element

    <div class="foo">I am in div</div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {
        console.log(JSON.stringify($(".nonexistingclass")));
      });
    </script>
    try it online
    The outcome from above code (in console) is:

    {"length":0,"prevObject":{"0":{},"context":{},"length":1},"context":{},"selector":".nonexistingclass"}
    

    jQuery Object for existing element

    <div class="foo">I am in div</div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {
        console.log(JSON.stringify($(".foo")));
      });
    </script>
    try it online
    The outcome from above code (in console) is:

    {"0":{},"length":1,"prevObject":{"0":{},"context":{},"length":1},"context":{},"selector":".foo"}
    

    Suggested posts:

    1. jQuery – how to get version info
    2. How to print javascript object to log
    3. jQuery how to load a url into an element
    4. Bash shell – check if file or directory exists
    5. jQuery – difference between html() and text()
    6. jQuery – check if element is visible in viewport ofter scroll
    7. jQuery encode string to html entities
    8. PHP check if key exists in array
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged HTML, Javascript, jQuery, Tutorials, Web Development

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress