Browser is chromium (under ubuntu)
This is a chunk of code: (of course) The alert messages shows the right value. but the points element doesn't get the right value, it actually gets empty. Can anybody tell me why?
Here's how points element is defined.
<input type='number' id='points' value='0'/>
and here is how javascript code is supposed to populate it.
alert(request.responseText);
document.getElementById("points").value=request.responseText;
In order to use a variable in document. getElementById() you simply add the variable name with no quotes. var my_variable = "field1"; document. getElementById(my_variable);
HTML DOM Document getElementById() The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
Your response is almost certainly a string. You need to make sure it gets converted to a number:
document.getElementById("points").value= new Number(request.responseText);
You might take a closer look at your responseText. It sound like you are getting a string that contains quotes. If you are getting JSON data via AJAX, you might have more consistent results running it through JSON.parse()
.
document.getElementById("points").value= new Number(JSON.parse(request.responseText));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With