...

View Full Version : focus not doing what i want



jeromepelletier
05-30-2006, 09:06 PM
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?

Kravvitz
05-30-2006, 09:35 PM
You want the cursor to go at the beginning of the text?

Try using this.

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();
}
}

jeromepelletier
05-31-2006, 10:16 PM
You want the cursor to go at the beginning of the text?

Try using this.

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 :)

Kravvitz
05-31-2006, 11:28 PM
Ah. Then try this.

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();
}
}

jeromepelletier
06-01-2006, 01:24 AM
Ah. Then try this.

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

Kravvitz
06-01-2006, 08:02 AM
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 (http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp).

jeromepelletier
06-01-2006, 01:06 PM
alright thanks :)

jeromepelletier
06-03-2006, 08:25 PM
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();
}

Kravvitz
06-04-2006, 03:50 AM
No, set it to a <input type="text"> or <textarea> element.

jeromepelletier
06-04-2006, 04:49 PM
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

Arty Effem
06-04-2006, 06:37 PM
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 linkThis seems to work on modern browswers:



<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>

jeromepelletier
06-04-2006, 11:53 PM
This seems to work on modern browswers:



<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????

januet
09-21-2007, 04:55 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum