aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes assign values to form elements dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "assign values to form elements dynamically?" Watch "assign values to form elements dynamically?" New topic
Author

assign values to form elements dynamically?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
I will invoke a java script method with arguments 1.form element name and 2.a String value which has to be assigned to that form element.

Can any one tell me how to do that?
i have done but it is giving error..
eg:--

<html>
<head>
<script type="text/javascript">
function convertText(element,value) {
document.SampleForm.element.value = value;
}
</script>
</head>
<body>
<form name="SampleForm">
<input type="text" name="myText">
<input type="button1" value="Convert" on click="convertText(button1,'Converted')">
<input type="button2" value="Convert" on click="convertText(button2,'Converted')">
<input type="button3" value="Convert" on click="convertText(button3,'Converted')">
<input type="button4" value="Convert" on click="convertText(button4,'Converted')">
</form>
</body>
</html>

i.e. when i click button1.....button1 value must change from convert to Converted..and similarly for other buttons..
which ever button i click its value must change to the value i passed as argument to the method.
[ August 19, 2007: Message edited by: Bear Bibeault ]

Abhishek
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Originally posted by Abhishek Reddy Chepyala:

i have done but it is giving error..


What error? How can you expect help to resolve the error if you don't tell us what it is?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The code you posted

is looking for a form element named element. In order to use an array, you need to use the elemetns array.

Eric
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply....iam looking for this

document.SampleForm.elements[element].value

thanks alot i got my problem solved...one more thing i want is once the button is clicked and after its value changed to Converted i want to disable that button.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks i got that one
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
i can able to disable the button and at the same time i want to display an image on the disabled button....can any one tell me how to do this.

document.main.elements[element].disabled=true;
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
Iam facing problem in hiding the form elements.The below code is working perfectly fine in InternetExplorer....but it is not working in mozilla firefox...can anyone tell me how to overcome this problem..

what iam doing is when the button value is changed from convert to converted
iam displaying an image beside the button...

<html>
<head>
<script>
function doThis(one,tick)
{
document.main.elements[one].value="added";
document.main.elements[one].disabled=true;
document.main.elements[tick].style.display="block";
}
</script>
</head>
<body>
<form name="main" action="get">
<img id="tick1" src="tick.gif" style="display:none"/><input type="button" name="one" value="add" on click="doThis('one','tick1')"/>
<img id="tick2" src="tick.gif" style="display:none"/><input type="button" name="two" value="add" on click="doThis('two','tick2')"/>
<img id="tick3" src="tick.gif" style="display:none"/><input type="button" name="three" value="add" on click="doThis('three','tick3')"/>
<img id="tick4" src="tick.gif" style="display:none"/><input type="button" name="four" value="add" on click="doThis('four','tick4')"/>
</form>
</body>
</html>
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
An img tag is not a form element. You need to reference that via

document.getElementById(tick).style.display="block";"

Eric
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks alot it worked fine..
one more thing is there any way to display the image on the button..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: assign values to form elements dynamically?