Solved

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

Posted on 2010-11-27
2
338 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 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