views:

50

answers:

3

Hey guys, I have this simple code that speaks for itself.Here it is:

enter code here 
<script language='javascript"> 
    function check() {} 
</script> 
<div id="a">input type="text" name="b"> 
<input type="button" onClick=" check(); ">

All i want is that when i press the button, the text field gets a value updated to it.

i tried using b.value=" C " but it doesnt seem to work.

Please advise. Any help will be appreciated.

Thanks.

+2  A: 
<script language='javascript"> 
     function check() {
          document.getElementById('txtField').value='new value here'
     } 
</script>

<input id="txtField" type="text" name="b"> <input type="button" onClick=" check(); ">

This will do. i gave it an ID, and use getElementById('txtField') using the id, and update it's value

Marcos Placona
+1  A: 

You seem to be thinking that giving a form input a name attribute makes it addressable as though it were a global variable. It doesn't. There is a syntax for that, and you would have to use something like:

document.forms[0].b.value = "C";

in order to get to address it successfully. You are putting your form elements inside a form, aren't you?

Do it that way, or use an ID along with the getElementById method, as mplacona suggests.

Robusto
+1  A: 

mplacona is got it... but have you ever thought about using jQuery!?

<script language="javascript">
$(document).ready(function(){
 $("#c").click(function(){
  $("#txtField").val("YOU NEW VALUE GOES HERE:)");
 });
});
</script>

<input id="txtField" type="text" name="b"> <input id="c" type="button" onClick=" check(); ">
Maxim Mai