• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

DYNAMICALLY refer to TEXTFIELDS & BUTTONS???

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an html page: on the left is a table of 60 buttons, on the right is a table of 40 textfields. When a textfield on the right gains focus, I need to set its background color yellow. Then when the user clicks on a button on the left, the text which is displayed on the button needs to replace the text in the textfield that I just changed the color in. The user is then able to update the value in the textfield by clicking on button with the appropriate text on the left.
I am having trouble doing this. My idea was to keep a var of the textfield which has most recently gained focus, and then set its value according to the button which is clicked, such as:
var btn = "";
var previousTxt = "";
function getStarted()
{
document.myform.lab1.focus();
}
function clickedButton(btnValue)
{
document.myform.btn.value = btnValue;
}
function focusTextbox(txt)
{
btn = txt;
}
function setColor(txt)
{
if (previousTxt != '')
document.myform.previousTxt.style.backgroundColor="white";
document.myform.txt.style.backgroundColor="yellow";
previousTxt = txt;
}
. . . set focus to the first textfield by default
<BODY BGCOLOR="#FFFFFF" onLoad="return getStarted();">
. . . button on the left:
<TD><input type=button value="Kd" onClick="return clickedButton('Kd');"></TD>
. . . textfield on the right:
<TD><INPUT size="5" type="text" name="lab1" value="WBC" OnFocus="setColor('lab1')"></TD>
---------------
When I try to refer to a textfield dynamically such as:
document.myform.btn.value = btnValue;
this does not work. I have also tried:
"document.myform." + btn + ".value" = btnValue;
If I do:
eval("document.myform." + txt + ".value")
I am able to see the value of the textfield. How do I dynamically refer to a given textfield? I have 40 textfields which need to be updated in the manner described above. I plan to use your solution to also dynamically refer to the selected button. The user will have up to 60 buttons to click on to update the textfields.
Your help is greatly appreciated!!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
to store to a textfield of a form, proceed like this:
var item = document.formname.textfieldname
you'll keep a reference to a textfield in item and could access to its value or change its properties.
hope my help doesn't come too late
 
He does not suffer fools gladly. But this tiny ad does:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!