Solved

adding a long text to textbox through javscript

Posted on 2008-06-24
10
891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 21861282
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
ID: 21861297
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
ID: 21870565
Thanks for the response. But I donot want to scroll text in a text area. My problem is within a textbox control.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Lolly-Ink
ID: 21870833
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
ID: 21870866
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
 
LVL 4

Expert Comment

by:msfletch
ID: 21870869
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
ID: 21879366
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
ID: 21879767
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
ID: 31470395
Many thanks
0
 
LVL 4

Expert Comment

by:msfletch
ID: 21879832
Glad to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 73
How do I add counter to html listbox using jquery 2 41
How to size popup in jquery mobile 4 57
Not picking up custom HTML-5 elements on page? 9 21
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
Suggested Courses

738 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