Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    focus not doing what i want

    In IE, this doesnt work too well but in FF it does.

    I have 1 iframe focusing onto a form in another iframe, it also inputs text...in IE the cursor goes before the text, in FF the cursor is after the text...this is what i want in IE..how do i fix it?

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You want the cursor to go at the beginning of the text?

    Try using this.
    Code:
    function placeCursorAtStart(el) {
      if (el.setSelectionRange) {
        el.setSelectionRange(0, 0);
      } else if (el.createTextRange) {
        var range = el.createTextRange();
        range.collapse(true);
        range.moveEnd('character', 0);
        range.moveStart('character', 0);
        range.select();
      }
    }
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kravvitz
    You want the cursor to go at the beginning of the text?

    Try using this.
    Code:
    function placeCursorAtStart(el) {
      if (el.setSelectionRange) {
        el.setSelectionRange(0, 0);
      } else if (el.createTextRange) {
        var range = el.createTextRange();
        range.collapse(true);
        range.moveEnd('character', 0);
        range.moveStart('character', 0);
        range.select();
      }
    }

    nope, i want the cursor at the end

  • #4
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah. Then try this.
    Code:
    function placeCursorAtEnd(el) {
      var len = el.value.length;
      if (el.setSelectionRange) {
        el.setSelectionRange(len, len);
      } else if (el.createTextRange) {
        var range = el.createTextRange();
        range.collapse(true);
        range.moveEnd('character', len);
        range.moveStart('character', len);
        range.select();
      }
    }
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #5
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kravvitz
    Ah. Then try this.
    Code:
    function placeCursorAtEnd(el) {
      var len = el.value.length;
      if (el.setSelectionRange) {
        el.setSelectionRange(len, len);
      } else if (el.createTextRange) {
        var range = el.createTextRange();
        range.collapse(true);
        range.moveEnd('character', len);
        range.moveStart('character', len);
        range.select();
      }
    }
    mind explaining some of it? I know minor javascript and thats a lil advanced for me :P

  • #6
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The function is passed a parameter/argument that is a reference to an element node; which in this case, is limited to <input type="text"> and <textarea> (and possibly <input type="password"> and <input type="file"> as well). The first line of the body finds the number of characters in the element's value. The second line of the body is used to detect if the browser supports Mozilla's method of selecting text. (Recent Mozilla browsers and Opera 8+ support it.) The third line uses that method to set the selection's start and end to after the last character. The fourth line detects if the browsers supports IE/Win's way of selecting text. The fifth through ninth lines of the function body does the same thing that the third line did but in a much more complicated way using IE/Win's TextRange object.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #7
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright thanks

  • #8
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it isnt focusing at all now, im suppose to change el to my form right ?

    for exmaple
    var el = IFRAme.FORM;
    var len = el.value.length;
    if (el.setSelectionRange) {
    el.setSelectionRange(len, len);
    } else if (el.createTextRange) {
    var range = el.createTextRange();
    range.collapse(true);
    range.moveEnd('character', len);
    range.moveStart('character', len);
    range.select();
    }
    Last edited by jeromepelletier; 06-03-2006 at 08:37 PM.

  • #9
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, set it to a <input type="text"> or <textarea> element.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #10
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kravvitz
    No, set it to a <input type="text"> or <textarea> element.
    im not following....i dont want it to do it when someones typing or anything, i want it to goto the end when someone clicks a link

  • #11
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by jeromepelletier
    im not following....i dont want it to do it when someones typing or anything, i want it to goto the end when someone clicks a link
    This seems to work on modern browswers:
    Code:
    <SCRIPT type='text/javascript'>
    
    function cursorToEnd( elem )
    {
     elem.focus();
     elem.value+='x';
     elem.value=elem.value.replace(/x$/,"");	
    }
    
    </SCRIPT>
    
     <A href='#' onclick='cursorToEnd(document.forms.myForm.myElement);return false;'>END</A>

  • #12
    Regular Coder
    Join Date
    Jul 2005
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arty Effem
    This seems to work on modern browswers:
    Code:
    <SCRIPT type='text/javascript'>
    
    function cursorToEnd( elem )
    {
     elem.focus();
     elem.value+='x';
     elem.value=elem.value.replace(/x$/,"");	
    }
    
    </SCRIPT>
    
     <A href='#' onclick='cursorToEnd(document.forms.myForm.myElement);return false;'>END</A>

    SWEET it worked! thanks so much, do you want credit on my site for this????

  • #13
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This post is kind of old, so don't know if you will get this. But, I just wanted to thank you for your code and you patient explanation of it. It was just what I needed for a Time & Attendence Edit log I was doing at work. Thanks again!
    januet


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •