29 users online (1 members and 28 guests)  


  Results 1 to 7 of 7

Related

  1. Opening a word document    Forum: Javascript Forum
    Replies: 6
  2. document.getElementById    Forum: Javascript Forum
    Replies: 0
  3. How to use getElementById with a body element    Forum: Javascript Forum
    Replies: 4
  4. Replies: 0
  1. #1
    Markol's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    4

    document.getElementByID

    I am sending a value to a js function from an onBlur event. This works fine and the function receives the value which is the name of a html textfield. I now want to use the document.getElementByID() function to set the text of the textfield to nothing.

    I use the following code to call the function from the textfield (works OK):
    Code:
    onBlur="yearFormat(courseStart.value, 'courseStart')
    This is the function:

    Code:
    function yearFormat(val,theID){
    if(val != ""){ 
      alert("error");
       if (document.getElementById){
    	  document.getElementByID(theID).value=" ";
        }
      }
    }
    When the onBlur event occurs I get the error:
    "Object doesn't support this property or method"

    Any help appreciated
    Mark

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: document.getElementByID()

    Hi there Markol,

    and a warm welcome to these forums.

    You have a 'typo' in this line...
    Code:
    document.getElementByID(theID).value=" ";
    ...it should, of course, be...
    Code:
    document.getElementById(theID).value=" ";

  3. #3
    Markol's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    4

    Re: document.getElementByID()

    Thanks a lot. I appreciate it.

    Sometimes the harder you stare at something the less you can see.

    One more question-
    In mozilla firefox the line
    Code:
    document.getElementById(theID).focus();
    doesnt set the focus to the textfield. Any ideas?

    Mark

  4. #4
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: document.getElementByID()

    Hi there Markol,

    I can see no reason for it not to work.

    Without seeing the full code that you are using it is impossible to point out the error.

  5. #5
    Markol's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    4

    Re: document.getElementByID()

    Here's the code. It all works except for in Netscape/Firefox the focus doesnt work.

    Code:
    function yearFormat(val,theID){
    var textID = theID;
    var re = new RegExp("(19|20)[0-9][0-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])"); 
    
    if(re.test(val) == false && val != ""){ //test for yyyy-mm-dd or not empty being false
      alert("The date format must be YYYY-MM-DD. Please try again");
       if (document.getElementById){//check for new browsers
    	  document.getElementById(theID).value="";
    	  document.getElementById(theID).focus();
        }
      }
    }

  6. #6
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: document.getElementByID()

    Hi there Markol,

    sorry about taking so long to get back on this one.

    It appears that 'Gecko' browsers are unable to re-focus blurred elements.
    I have spent hours trying to think of a fix.
    This is what I finally came up with...
    Code:
    <script type="text/javascript">
    <!--
    function yearFormat(val,theID){
      var textID = theID;
      var re = new RegExp("(19|20)[0-9][0-9]-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])"); 
    
    if(re.test(val) == false && val != "") {     //test for yyyy-mm-dd or not empty being false
      alert("The date format must be YYYY-MM-DD. Please try again");
    if(document.getElementById) {    //check for new browsers
           document.getElementById(theID).value="";
    
    //required fix for Gecko browsers
    
    mozBug=theID; 
       setTimeout("document.getElementById(mozBug).focus()",0);
        }
      }
    }
    //-->
    </script>
    
    <form action="#">
    <div>
    <input id="courseStart" type="text" onBlur="yearFormat(this.value, 'courseStart')">
    </div>
    </form>

  7. #7
    Markol's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    4

    Re: document.getElementByID()

    Thank you for your time and effort. Its very good of you.