Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting HTML textbox value using javascript function?

I'm using this code to set the HTML textbox value using Javascript function. But it seems to be not working. Can anyone point out, what is wrong with this code?

Whats your Name?
<input id="name" value="" />

<script type="text/javascript"> 
function setValue(value){
var myValue=value;
document.getElementsById("name").value = myValue;
}
</script>

the "value" is came from my android java class using this codes

String value = "Isiah";
    WebView web = (WebView) findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("file:///android_asset/www/webpage");
    web.loadUrl("javascript:setValue("+ value +")");
like image 624
user1570222 Avatar asked Apr 14 '26 18:04

user1570222


2 Answers

   function setValue(value) {
    var myValue=value; //unnecessary
    document.getElementById("name").value= myValue;
}

But then as pointed out in the comments, you need to call setValue(value) somewhere in your code. Right now you just defined the function is never called.

like image 128
Hazem Salama Avatar answered Apr 17 '26 06:04

Hazem Salama


You could either access the element’s value by its name:

  document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
    document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"

So:

    input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />

Or you assign an ID to the element that then identifies it and you can access it with getElementById:

    <input name="textbox1" id="textbox1" type="text" />
    <input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />
like image 43
Venkatesh S Avatar answered Apr 17 '26 06:04

Venkatesh S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!