3

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 +")");
8
  • document.getElementsByName("name").value = value Commented Aug 2, 2012 at 4:00
  • What's with the extra " after \"Javascript\"> ? (and the backslashes) Commented Aug 2, 2012 at 4:01
  • First you store the value passed to the function in a var (unnecessary) then try to overwrite it with a DOM element's value and do nothing more. I said "try" because getElementsByName returns a HTMLCollection and not a DOM element, let alone you don't have any element with that name. Use getElementById instead. Also, you don't ever call the function. It'd be better if you explicitly tell us what you're expecting your function to do. Commented Aug 2, 2012 at 4:03
  • It looks like your assignment is reversed Commented Aug 2, 2012 at 4:09
  • 1
    You have a typo. getElementsById should be getElementById (without the extra s). Also the function is never called. Commented Aug 2, 2012 at 4:12

8 Answers 8

7
   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.

Sign up to request clarification or add additional context in comments.

2 Comments

how can I call it without using a button?
You can use a button/link/or just call the function on page load. Depends on when you want the box to be filled. When do you want this to happen?
1

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" />

Comments

0

You are using document.getElementsById("name") it should be document.getElementById("name")

not Elements it is Element

Comments

0

You are not linking the function to anything. For example, a click:

<input id="name" value="" onclick="javascript:this.value=12;"/>

Replace the onclick attribute for your desired function, whatever it does (you need to be more specific)

Also, there is no language attribute (at least not anymore) use type="text/javascript" instead

Here is a fiddle: http://jsfiddle.net/4juEp/

Click the input to see it working.

Look at this second fiddle. http://jsfiddle.net/4juEp/1/

which loads whatever is defined in the hid input to the name input.

6 Comments

how can I call it without using a button?
@user1570222 I've never used Android (I plan to but I don't know any of it right now) See the link I updated to the answer, hope that it helps, as I said, you need call the function from somewhere. Take a lesson on javascript youtube.com/watch?v=v2ifWcnQs6M
@user1570222 Well, what exactly do you need? execute it when the page is loaded?
@user1570222 and where from do you take the input to the function? myValue in your example.
myValue = value. "value" is came from my android java class
|
0

Firstly, you have a typo in your javascript function i.e. you have used getElementsById as compared to getElementById

To set the value of the textbox on page load, I suggest you use an alternative

<body onload="setValue('yourValueToSet');">

<!-- Your usual html code in the html file -->

</body>

Comments

0

I think you are missing the quotes,

try,

web.loadUrl("javascript:setValue('"+ value +"')");

also consider about the typo.

3 Comments

I edited it to web.loadUrl("javascript:setValue('"+ value +"')"); still undefined
i'm not sure whether you are missing the html extension at the end of web.loadUrl("file:///android_asset/www/webpage"); or try web.loadUrl("javascript:setValue(\""+ value +"\")");
I already change it like what you said but still its undefined
0

Check this out:

<body onload="setvalue($value);">
Whats your Name?<input id="name" name="name" value=""/>
<script type="text/javascript">
function setValue(value){
document.{formname}.name.value = value;}</script>

Comments

-1

It's not Elements It's Element

You should use document.getElementById('object-id');

1 Comment

Try to elaborate your answer. Like giving code or documentation etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.