updating input field with javascript
Hello,
I'm trying to update the value of an input text field using javascript. The script I wrote works fine in IE, but it does not work on Firefox.
As you can see it's a very simple scritp that must add the char '2' at the end of the string that It will be on the input field. If I do the following it does not work:
At the beggining, the input text field has no value and if I press the button several times the input text field updates correctly whit the string '2222...' but If NOW I edit the value, i.e '22223', I would expect to have '222232' after pressing the button but nothing happens and the input field is not updated.
Anyone could help me?
Thanks in advance
<html>
<head>
<title>Prueba</title>
<script language="JavaScript">
function do_update(objeto){
valorActual = document.getElementById(objeto).value;
valorActual = valorActual+2;
document.getElementById(objeto).setAttribute("value",valorActual);
}
</script>
</head>
<body>
<input type="text" name="0" value="" maxlength="50" size="50" id="p0">
<input type="button" class="botoncorto" value="+2" onClick="do_update('p0');">
</body>
</html>
[1416 byte] By [
salbefe] at [2007-11-11 11:57:46]

# 1 Re: updating input field with javascript
Simply because IE ignore bugs !
it was coded by a base of "on ANY error resume NEXT" !!!
try this :
document.getElementById(objeto).value = valorActual;
Amahdy at 2007-11-11 23:34:09 >

# 2 Re: updating input field with javascript
Amahdy thank you very much for your reply. It works fine with your code. I thought that setAttribute was a standart function that should work with all browser.
When is supposed that We should use setAttribute and when the code you have written?
Thank you.
# 3 Re: updating input field with javascript
I wrote my suggestion about JS here :
http://amahdy.blogspot.com/2007/10/amahdy-why-im-interested-with-office.html
I don't know all the standard language, but I know that IE ignore many things while parsing, and that's sometimes good, but most of time tends to the non-standardized language of JS and make problems for programmers more than helping them.
I don't know a standard answer too, but generally it's something like following the OOP we set the member data of an object after a dot and the object definition :
object.data_member
and value once edited by the user shouldn't be an attribute but a data member only ...
Amahdy at 2007-11-11 23:36:12 >

# 4 Re: updating input field with javascript
Hi salfbefe,
The value attribute is (as an attribute, not a property) in the HTML is meant to provide an initial value to the text box / field. It is not valid after you start editing the field. That is what happens with FF / Netscape. The value property (which is available via a scripting language like JavaScript), on the other hand, is linked to the value in the text box. So updating it will reflect on the textbox always, and as the text box is edited, it will also be updated. You may also have noticed that getting the value attribute via getAttribute will NOT give you the value in the text box, but the initial value in the value attribute or the value you set with setAttribute.