Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

adding a long text to textbox through javscript

Posted on 2008-06-24
10
Medium Priority
?
899 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

916 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