Solved

adding a long text to textbox through javscript

Posted on 2008-06-24
10
880 Views
Last Modified: 2010-04-21
I have a textbox control in my page and I set the value of this textbox using javascript. Sometimes the text is too long and  the textbox shows the text from the start. Is there a way to make the textbox show the end part of the text after the value is set?

for example

var str="this is my very long string that I want to put inside a smaller textbox control";
var txtbox = document.getElementById('txtbox');
txtbox.value= str;
txtbox.focus();

If I use the above script to set the value in a text box control with id="txtbox". The size of text control is 35. after the script is run, I see
"this is my very long string that I w" in the textbox and rest of the string is hidden. I need to navigate through the letters to see them. is there a way to make the txtbox to show  "inside a smaller textbox control"  that is the last part visible and the first part hidden instead?

0
Comment
Question by:zmoidin
10 Comments
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
IE only.  There may be a method that is W3C cross browser compliant, but I don't know of one.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Text Select</title>

<script type="text/javascript">

<!--

function fireScroll(taObj) {

	taObj.doScroll('scrollbarPageDown');

}

//-->

</script>

</head>
 

<body>

<form id="form1" name="form1" method="post" action="null">

  <textarea id="ta1" name="ta1" rows="5"  cols="50" onmouseup="selectText(this)">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras dolor. Sed sed neque. Duis eros justo, bibendum in, placerat eget, ullamcorper vel, nibh. Praesent urna arcu, sagittis eget, semper laoreet, tempus non, augue. In a pede. Vestibulum consequat eleifend sem. Duis tristique tortor ultricies massa. Duis sapien eros, posuere sit amet, rhoncus non, venenatis bibendum, tellus. Donec dui tortor, dignissim quis, eleifend eget, fermentum a, massa. Maecenas vel enim vitae sapien condimentum eleifend. Duis hendrerit luctus dui.</textarea>

</form>

<p><a href="javascript:void(0);" onclick="fireScroll(document.getElementById('ta1'));">Scroll to bottom</a></p>

</body>
 

</html>

Open in new window

0
 
LVL 4

Expert Comment

by:msfletch
Comment Utility
This link may be helpful with other browsers. It is a thread specifically about identifying cursor location and setting it:

http://blog.vishalon.net/Post/57.aspx
0
 

Author Comment

by:zmoidin
Comment Utility
Thanks for the response. But I donot want to scroll text in a text area. My problem is within a textbox control.
0
 
LVL 16

Expert Comment

by:Lolly-Ink
Comment Utility
The solution is to make sure the text box is empty when you focus it, then set the value.

Here is a working example based on your code:

<html>

<script>

   function load()

   {

      var str    = "this is my very long string that I want to put inside a smaller textbox control";

      var txtbox = document.getElementById('txtbox');

      if (txtbox)

      {

         txtbox.focus();

         txtbox.value = str;

      }

   }

</script>

<body onload="load()">

   <input type="text" name="txtbox" size="35">

</body>

</html>

Open in new window

0
 
LVL 4

Expert Comment

by:msfletch
Comment Utility
Setting the caret after the insert solves the problem. View attached code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 

<html>

<head>

	<title>Untitled</title>
 

	<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

    <!--

	

    	function insertLongText() {

			var str="this is my very long string that I want to put inside a smaller textbox control";

			var txtbox = document.getElementById('txtbox');

			txtbox.value= str;

			setCaretPosition('txtbox', 10000);

		}

		

		function setCaretPosition(elemId, caretPos) {

		

	    var elem = document.getElementById(elemId);
 

		    if(elem != null) {

		        if(elem.createTextRange) {

		            var range = elem.createTextRange();

	    	        range.move('character', caretPos);

		            range.select();

		        } else {

		            if(elem.selectionStart) {

		                elem.focus();

		                elem.setSelectionRange(caretPos, caretPos);

		            } else {

						elem.focus();

					}

				}

		    }

		}

		

    //-->

    </SCRIPT>
 

</head>
 

<body>
 

<form name="form" method="post" action="some_page.cfm">
 

<input type="text" name="txtbox" id="txtbox" size=50>
 

<input type="button" value="insert text" onClick="insertLongText();">
 

</form>
 
 

</body>

</html>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:msfletch
Comment Utility
By the way ... above solution originally from:

http://blog.vishalon.net/Post/57.aspx

Setting the "10000" to whatever value is larger than the text you are entering sets the cursor (and therefore the focus) to the end of the text field.
0
 

Author Comment

by:zmoidin
Comment Utility
msfletch,

Your solution works well in IE, however it doesnot do anything in firefox. Do you have any idea what to do for firefox? I see that you have handled the case for firefox in the code, but it is not selecting the last position.
0
 
LVL 4

Accepted Solution

by:
msfletch earned 125 total points
Comment Utility
zmoidin,

The code is actually working the way it is intended, meaning that the cursor is being sent to the end of the text field in both cases (IE & FF).

You can test this by running the code, then holding SHIFT and clicking in the middle of the text. Notice how the selection of the text starts from the end instead of the beginning. You can also simply start typing and the focus will move to the end of the field.

The problem here is that FF has a known bug related to drawing cursors in form fields, which keeps it from moving the display of the content of the field to show the cursor. More info here:

http://www.google.com/search?q=firefox+show+cursor+in+field&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

There are a lot of suggestions on workarounds. I have tried a few and will keep looking at it to see if I can figure it out.

Anyone else?
0
 

Author Closing Comment

by:zmoidin
Comment Utility
Many thanks
0
 
LVL 4

Expert Comment

by:msfletch
Comment Utility
Glad to help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now