Category: jQuery

  • Get textfield value if textfield exist using jquery

    Get textfield value if textfield exist using jquery

    Check if element exists in jQuery

    Solution Over Google

    Try to check the length of the selector, if it returns you something then the element must exists else not.

     if( $('#selector').length )         // use this if you are using id to check
    {
         // it exists
    }
    
    
     if( $('.selector').length )         // use this if you are using class to check
    {
         // it exists
    }

    My Solution

    <html>
    <head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    </head>
    <body>
    <form  method="post">
    <input type="text" name="myTextField" id="myTextField" />
    <input type="text" name="myTextField1" id="myTextField1" />
    <input type="text" name="myTextField2" id="myTextField2" />
    <input type="text" name="myTextField3" id="myTextField3" />
    <input type="button" name="b1" id="b1" value="Click" class="button" />
    </form>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script language="javascript">
      $('#b1').click(() => {
      
      if( $('#myTextField4').length )         // use this if you are using id to check
    {
          alert('sadasdsa');
    }
    else
    {
    	alert('this text field not found');
    }
             
    
    });
    </script>
    </body>
    </html>