Solved

Update textbox with value written in a file (Simulate as if user has entered it)

Posted on 2010-11-27
2
339 Views
Last Modified: 2012-05-10
Please let me know how I can create an HTML page with a text box that updates its value periodically (say every 2 seconds) with a value written in a text file.
The code attached does this for me.

I now need one additional feature - I need to simulate the user entering the value (from the file) into the text box by simulating the following procedure:

1. Clicking on the textbox to change focus
2. Entering the value (read from the file)
3. Clicking outside the textbox to remove focus (The click event must be simulated)

PS: The click event must be simulated. It is not enough if just the focus is changed.

If possible, please provide some sample code. Thanks!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script language="javascript">
	setInterval( '$("#textboxID").val( $.ajax({"url":"file.text", "async":false}).responseText );', 2000 );
</script>
</head>
<body>
<input id="textboxID" />
</body>
</html>

Open in new window

0
Comment
Question by:dshrenik
2 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 250 total points
ID: 34224352

Are you looking for something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26641836.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script language="javascript">

var textBox = null;
var index = 0;
var text = "";

function update( box, value ) {
  if ( arguments.length > 1 ) {
    textBox = box;
    text = value;
    textBox.focus();
    index = 0;
  }
  textBox.val( text.substr( 0, ++index ) );
  setTimeout( "update()", 333 );

  if ( index == text.length ) textBox.blur();
}

function repeat() {
  update( $("#textboxID"), $.ajax({"url":"https://updates.opendns.com/nic/update", "async":false, "username":"bogus", "password":"bogus"}).responseText );
}

$(document).ready( function() {

  update( $("#textboxID"), "Loading..." );
  setInterval( 'repeat()', 4000 );

});
</script>
</head>
<body>
<input id="textboxID" />
</body>
</html>

Open in new window

0
 
LVL 16

Assisted Solution

by:jmatix
jmatix earned 250 total points
ID: 34232222
Try this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script language="javascript">
$(document).ready(function(){
	var txt =  $.ajax({"url":"file.text", "async":false}).responseText;
	$("#textboxID").focus();
	$._int = setInterval("setText('"+txt+"')", 300 );
});

function setText(txt)
{
	var t = $("#textboxID").val();
	if (t.length==txt.length)
	{
		clearInterval($._int);
		$("#textboxID").blur();
		return;
	}
	$("#textboxID").val(txt.substr(0, t.length+1));
}


</script>
</head>
<body>
<input id="textboxID" />
</body>
</html>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

821 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