Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. focus() not working

focus() not working

Scheduled Pinned Locked Moved JavaScript
testingbeta-testinghelp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RabbitTrail
    wrote on last edited by
    #1

    Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

    Trying It Out
    function checkForm()
    {
    var str = ''; //for testing purposes later
    var e = document.getElementById('myForm').elements;

       //Insert for statement here//
    	for(i=0; i <= e; i++){
    		if(e\[i\].value.length <1){
    			alert("The field " + e.\[i\].name + " is blank");
    			var mytext = document.getElementById(e\[i\].name)
    			mytext.focus();
    			return false;
    			
    		}
    	}
    
     }
    

    Your Name:

    Interesting Fact About You:

    something about you


    P B V S B 5 Replies Last reply
    0
    • R RabbitTrail

      Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

      Trying It Out
      function checkForm()
      {
      var str = ''; //for testing purposes later
      var e = document.getElementById('myForm').elements;

         //Insert for statement here//
      	for(i=0; i <= e; i++){
      		if(e\[i\].value.length <1){
      			alert("The field " + e.\[i\].name + " is blank");
      			var mytext = document.getElementById(e\[i\].name)
      			mytext.focus();
      			return false;
      			
      		}
      	}
      
       }
      

      Your Name:

      Interesting Fact About You:

      something about you


      P Offline
      P Offline
      Peter Leow
      wrote on last edited by
      #2

      You have made 3 mistakes in your javascript:

      i <= e

      should be

      i < e.length

      and

      e[i].value.length <1

      should be, use trim to prevent user from entering all white space

      e[i].value.trim().length < 1

      and

      var mytext = document.getElementById(e[i].name)

      should be

      var mytext = e[i];

      See the corrected code:

      function checkForm()
      {
      var str = ''; //for testing purposes later
      var e = document.getElementById('myForm').elements;

      //Insert for statement here
      for(i=0; i < e.length; i++){
      if(e[i].value.trim().length < 1){
      alert("The field " + e[i].name + " is blank");
      var mytext = e[i];
      mytext.focus();
      return false;
      }
      }
      }

      Remember to accept this answer and vote.

      R 1 Reply Last reply
      0
      • P Peter Leow

        You have made 3 mistakes in your javascript:

        i <= e

        should be

        i < e.length

        and

        e[i].value.length <1

        should be, use trim to prevent user from entering all white space

        e[i].value.trim().length < 1

        and

        var mytext = document.getElementById(e[i].name)

        should be

        var mytext = e[i];

        See the corrected code:

        function checkForm()
        {
        var str = ''; //for testing purposes later
        var e = document.getElementById('myForm').elements;

        //Insert for statement here
        for(i=0; i < e.length; i++){
        if(e[i].value.trim().length < 1){
        alert("The field " + e[i].name + " is blank");
        var mytext = e[i];
        mytext.focus();
        return false;
        }
        }
        }

        Remember to accept this answer and vote.

        R Offline
        R Offline
        RabbitTrail
        wrote on last edited by
        #3

        Thank you for pointing out my mistakes. I modified my code to include your corrections but the focus still does not put the cursor in the blank text or textarea continters. Any other suggestions? I have tried the code in IE, Firefox and Chrome.

        P 1 Reply Last reply
        0
        • R RabbitTrail

          Thank you for pointing out my mistakes. I modified my code to include your corrections but the focus still does not put the cursor in the blank text or textarea continters. Any other suggestions? I have tried the code in IE, Firefox and Chrome.

          P Offline
          P Offline
          Peter Leow
          wrote on last edited by
          #4

          Try the full example:

          <!DOCTYPE html>
          <head>
          <meta "charset=utf-8" />
          <title>Trying It Out</title>
          <script type="text/javascript">
          function checkForm()
          {
          var str = ''; //for testing purposes later
          var e = document.getElementById('myForm').elements;

          //Insert for statement here
          for(i=0; i < e.length; i++){
          if(e[i].value.trim().length == 0){
          alert("The field " + e[i].name + " is blank");
          var mytext = e[i];
          mytext.focus();
          return false;
          }
          }
          }
          </script>
          </head>

          <body>

          <form id="myForm" name="myForm" action="showIt.htm">
          <p>
          Your Name:

          <input type="text" name="Your_Name" value="enter your name" />
          </p>
          <p>
          Interesting Fact About You:

          <textarea name="Interesting_Fact">something about you</textarea>
          </p>
          <input type="submit" value="Submit Data" onclick="return checkForm();" />

          </form>
          <hr />
          <div id="showTheValues"></div>
          </body>
          </html>

          1 Reply Last reply
          0
          • R RabbitTrail

            Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

            Trying It Out
            function checkForm()
            {
            var str = ''; //for testing purposes later
            var e = document.getElementById('myForm').elements;

               //Insert for statement here//
            	for(i=0; i <= e; i++){
            		if(e\[i\].value.length <1){
            			alert("The field " + e.\[i\].name + " is blank");
            			var mytext = document.getElementById(e\[i\].name)
            			mytext.focus();
            			return false;
            			
            		}
            	}
            
             }
            

            Your Name:

            Interesting Fact About You:

            something about you


            B Offline
            B Offline
            Blikkies
            wrote on last edited by
            #5

            It could be the syntax error, add a semicolon at the end of this line:

            var mytext = document.getElementById(e[i].name)

            1 Reply Last reply
            0
            • R RabbitTrail

              Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

              Trying It Out
              function checkForm()
              {
              var str = ''; //for testing purposes later
              var e = document.getElementById('myForm').elements;

                 //Insert for statement here//
              	for(i=0; i <= e; i++){
              		if(e\[i\].value.length <1){
              			alert("The field " + e.\[i\].name + " is blank");
              			var mytext = document.getElementById(e\[i\].name)
              			mytext.focus();
              			return false;
              			
              		}
              	}
              
               }
              

              Your Name:

              Interesting Fact About You:

              something about you


              V Offline
              V Offline
              venkatpvc
              wrote on last edited by
              #6

              Please note the changes...

              <html lang="en">
              <head>
              <meta "charset=utf-8" />
              <title>Trying It Out</title>
              <script type="text/javascript">
              function checkForm()
              {
              var str = ''; //for testing purposes later
              var e = document.getElementById("myForm").elements;
              for (var i = 0; i < e.length; i++) {
              var val = e[i].value;
              if (val.length == "" ) {
              alert("The Field " + e[i].name + " is blank")
              e[i].focus();
              return false;
              }
              }
              }
              </script>
              </head>

              <body>

              <form id="myForm" name="myForm" action="showIt.htm">

              Your Name:

              <input id="Your_Name" type="text" name="Your_Name" value="enter your name" />

              Interesting Fact About You:

              <textarea id="Interesting_Fact" name="Interesting_Fact">something about you</textarea>

              <input type="submit" value="Submit Data" onclick="return checkForm()" />

              </form>


              </body>
              </html>

              1 Reply Last reply
              0
              • R RabbitTrail

                Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

                Trying It Out
                function checkForm()
                {
                var str = ''; //for testing purposes later
                var e = document.getElementById('myForm').elements;

                   //Insert for statement here//
                	for(i=0; i <= e; i++){
                		if(e\[i\].value.length <1){
                			alert("The field " + e.\[i\].name + " is blank");
                			var mytext = document.getElementById(e\[i\].name)
                			mytext.focus();
                			return false;
                			
                		}
                	}
                
                 }
                

                Your Name:

                Interesting Fact About You:

                something about you


                S Offline
                S Offline
                Sunasara Imdadhusen
                wrote on last edited by
                #7

                Please use following solution. I have verified and it is working fine for textbox.

                function checkForm()
                {
                var str = ''; //for testing purposes later
                var e = document.getElementsByTagName('input');

                  //Insert for statement here//
                   for(i=0; i <= e.length; i++){
                       var type = e\[i\].getAttribute('type');
                       if(type == "text" || type == "textarea"){
                           if(e\[i\].value.length <1){
                               alert("The field " + e\[i\].name + " is blank");
                               e\[i\].focus();
                               return false;
                           }
                       }
                   }
                }
                
                R 1 Reply Last reply
                0
                • R RabbitTrail

                  Below is code I have been trying. Everything works except the focus. When the alert box's OK is clicked, focus is not set to the blank text/textarea control that caused the error to happen. According to articles online, this should work but it is not. Any suggestions would be greatly appreciated.

                  Trying It Out
                  function checkForm()
                  {
                  var str = ''; //for testing purposes later
                  var e = document.getElementById('myForm').elements;

                     //Insert for statement here//
                  	for(i=0; i <= e; i++){
                  		if(e\[i\].value.length <1){
                  			alert("The field " + e.\[i\].name + " is blank");
                  			var mytext = document.getElementById(e\[i\].name)
                  			mytext.focus();
                  			return false;
                  			
                  		}
                  	}
                  
                   }
                  

                  Your Name:

                  Interesting Fact About You:

                  something about you


                  B Offline
                  B Offline
                  blachsmith
                  wrote on last edited by
                  #8

                  e.[i].name what's the fucking dot between e and [i] for?

                  1 Reply Last reply
                  0
                  • S Sunasara Imdadhusen

                    Please use following solution. I have verified and it is working fine for textbox.

                    function checkForm()
                    {
                    var str = ''; //for testing purposes later
                    var e = document.getElementsByTagName('input');

                      //Insert for statement here//
                       for(i=0; i <= e.length; i++){
                           var type = e\[i\].getAttribute('type');
                           if(type == "text" || type == "textarea"){
                               if(e\[i\].value.length <1){
                                   alert("The field " + e\[i\].name + " is blank");
                                   e\[i\].focus();
                                   return false;
                               }
                           }
                       }
                    }
                    
                    R Offline
                    R Offline
                    RabbitTrail
                    wrote on last edited by
                    #9

                    Thank you. I works fine.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups