tables and javascript

java , javascript United States
  • 18 years ago

    I built a page in it I placed a table in each cell in the table (not sure of the correct term) I placed an
    <input type=text> on some of the textfields I placed an onBlur event. In the onBlur event is an attempt (using JavaScript) to focus on a textfield in the table. I cannot seem to get the textfield focused. I can change the value or make the value NULL. I can loop threw all the elements in each form I can capture the value, name or just about anything else about the textfeilds I can even select the text in the textfield but try to focus and no way can I do this while it is in a table. Yet out of the table no problem.


    I am sure my syntax contains a simple error but I cannot find it in any book or on the web. Do you know where I can go to find the error in my syntax?


    function reFocus(txtName)
    {   for(x=0;x<document.forms[0].elements.length;x++)
      {     if(document.forms[0].elements[x].name==txtName.name)
        {  
         document.forms[0].elements[x].select();                    
         document.forms[0].elements[x].focus();
         break;
        }
      }
    }


    <tabel>
    <tr bgcolor='#D9D9DD'>
    <td width='60%'>Home Page</td>
    <td width='40%'><input name='HomePage' type='text' onFocus='reFocus(this)' value=''size='15'></td>
    </tr>
    </table>

  • 18 years ago

    Let me see if I understand right: You want to make it so that when a user tries to move the cursor out of the field, the onBlur event fires and re-focuses it, so they can't leave the field? If so, two things come to mind.


    First, in the HTML code you show, you are calling the reFocus function on the onFocus event... shouldn't it be the onBlur event?


    Second, you can make it much simpler by passing the this object to the function in the onBlur event, like this:

    Code:
    onBlur="reFocus(this)"

    Then the reFocus function becomes:
    Code:
    function reFocus(obj){
        obj.focus();
    }


    Hope that helps.

  • 18 years ago

    Thanks for the help


    I am sorry I pasted the incorrect JS function, I do have an onBlur event as well the onFocus event was just to try and focus the privious textfield in the table. I simply cannot find a way to focus the textfield in a table using JavaScript


    Here is the URL http://www.wyght.com/insert.php


    I have remed out all but the focus part of the JS function and still it will not work


    If anyone can tell me what I am doing incorrectly please do.


    Warren


  • 18 years ago

    I think you need to make a few small changes to get this code working correctly. Try this version of your "doThis" function


    Code:

    function doThis(txtSrc)  {
     if(txtSrc.value=="") {
       txtSrc.focus();
     } else {
       document.form1.elements[0].value="testing";
       document.form1.elements[1].value=document.form1.elements[1].name;
       document.form1.elements[0].select();
       document.form1.elements[0].focus();
     }
    }


    notice that you only need to parse the object to the function, so your onBlur code should look like this


    Code:
    onBlur="doThis(this);"


    not entirely sure what your trying to do with this code though so what I have done might not be what your after.


    hope it helps anyway

  • 18 years ago

    I have two textfields in my form I want to validate, one is a password one is an email. When the client leaves either of those boxes I want to do a quick check using a confirm box to allow the user to confirm their entry. The code below works extreemly well when I have no table involved, once a table holds the textfields I want to validate forget the focus, I can seemingly do anything to that textfield save focus on it.


    Code:

    onBlur=doThis(this)


    <script>


    function doThis(obj)
     {
        if(obj.value != confirm("Please re-enter your " + obj.name))
         {
            alert("Sorry there was an error, please enter your " + obj.name+" again.");
             obj.value="";
             obj.focus();
          }
       }
    </script>




Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook