Document.getElementById (). value does not set value

Browser - Chrome (under ubuntu)

This is a piece of code: (of course). Warning messages show the correct value. but the point the element does not get the correct value, it actually becomes empty. Can someone tell me why?

The point element is defined here.

<input type='number' id='points' value='0'/> 

and this is how javascript code should be used.

 alert(request.responseText); document.getElementById("points").value=request.responseText; 
+7
source share
5 answers

Your answer is almost certainly a string. You need to make sure that it is converted to a number:

 document.getElementById("points").value= new Number(request.responseText); 

You can consider your answer in more detail. It looks like you are getting a string containing quotation marks. If you get JSON data through AJAX, you can get more consistent results with JSON.parse() .

 document.getElementById("points").value= new Number(JSON.parse(request.responseText)); 
+10
source

According to my tests with Chrome:

If you set the input number to a number, then it works fine.

If you set the input number to a string that contains only a number, then it works fine.

If you set the input number to a string containing a number and some spaces, it will close the input.

You probably have a space or a new line after the data in the server response that you really like.

Use document.getElementById("points").value = parseInt(request.responseText, 10); instead of this.

+3
source

The only case I could imagine is that you run it in a webkit browser like Chrome or Safari, and the return value in responseText contains a string value.

In this structure, the value cannot be displayed (it will be empty)

Example: http://jsfiddle.net/BmhNL/2/


My point is that I expect an incorrect / double encoded string value. Webkit browsers are more stringent on type = number . If there is a β€œonly” problem with a space, you can try to implicitly call the Number() constructor, for example

 document.getElementById("points").value = +request.responseText; 
+1
source

The problem is clearly not with javascript. Here is a quick snippet showing you how to work with the code.

 document.getElementById('points').value = 100; 

http://jsfiddle.net/eqTm2/1/

As you can see, javascript perfectly assigns a new value to the input element. It would be helpful if you could talk more about this.

0
source

Try using

 $('#points').val(request.responseText); 
0
source

All Articles