...

View Full Version : interesting script here, anyone know how they did it?



canadianjameson
05-24-2005, 10:55 PM
hey. a while back i was asking if it were possible to have the cursor in a textbox focus onload to a specific position in the text (that is defaulted in the textbox). in this case i wanted to know if i could have it default at the back of the text instead of the front.

it seems they have found a way to do it here:
https://www1.royalbank.com/cgi-bin/rbaccess/rbunxcgi?F6=1&F7=IB&F21=IB&F22=IB&REQUEST=ClientSignin&LANGUAGE=ENGLISH

any idea how they managed it?

Mr J
05-24-2005, 11:09 PM
Not too sure here but take a look at the following 2 examples.

The first one puts the cursor at the beginning, the second at the end


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY onload="document.focusing1.focused.focus()">

<form name="focusing1">
<input type="text" name="focused" value="Focused" size=20>
</form>

</BODY>
</HTML>


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY onload="with(document.focusing2.focused){focus();value +=' '}">

<form name="focusing2">
<input type="text" name="focused" value="Focused" size=20>
</form>

</BODY>
</HTML>

EDIT.

Just ran it through Moz and found the cursor is at the end in both examples yet IE shows differently Bah!

canadianjameson
05-24-2005, 11:58 PM
hehe, you get a cookie for the effort :thumbsup:

:D

enumerator
05-25-2005, 04:27 AM
It's not a "cursor", but a "caret". :)


<input value="blahblahblah" onfocus="(function(txt)
{
if(!txt)
return;
(txt = txt()).collapse(false);
txt.select();
})(createTextRange)">

Not sure if that will escape error in non IE-compliant agents, but...

rlemon
05-25-2005, 04:46 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script>
function set(id){
var txt = document.getElementById(id);
var txtValue = txt.value;
txt.focus();
txt.value = txtValue;
txt.focus();
}
</script>
</head>

<body onload="set('txt')">
<input type="text" id="txt" name="txt" value="test">
<input type="button" onclick="set('txt')" value="set caret to end">
</body>
</html>


works in ie and mozilla.

enumerator
05-25-2005, 04:58 AM
Except with onfocus... :p

rlemon
05-25-2005, 05:52 AM
Except with onfocus... :p
i don't know what your talking about, in ie and mozilla it works.

set it onload and you don't need onfocus.

enumerator
05-25-2005, 06:22 AM
That's what I was talking about: "in ie and mozilla it works", except with onfocus. No big deal, just pointing out that it works only from an external event, so to speak... :)

rlemon
05-25-2005, 07:33 AM
well, body onLoad="" works so w/e

glenngv
05-26-2005, 06:55 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<script>
function set(id){
var txt = document.getElementById(id);
var txtValue = txt.value;
txt.focus();
txt.value = txtValue;
txt.focus();
}
</script>
</head>

<body onload="set('txt')">
<input type="text" id="txt" name="txt" value="test">
<input type="button" onclick="set('txt')" value="set caret to end">
</body>
</html>


works in ie and mozilla.
That partially works in Firefox and probably in Moz too. If you set the caret anywhere but the end, the caret will not go to the end when the button is clicked. Tested it in Opera and didn't work at all.

But you can minimize the code further. The important trick there is focusing first before setting the value.

function set(id){
var txt = document.getElementById(id);
txt.focus();
txt.value = txt.value;
}

rlemon
05-26-2005, 04:55 PM
yea, thats why I just stay away from setting the caret.

Made a wysiwyg editor, had a ******* of a time to get that working properly. Still isn't perfect, but w/e.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum