Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.getElementById().value doesn't set the value

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;
like image 505
Not Amused Avatar asked Nov 25 '12 17:11

Not Amused


People also ask

How do you set the document getElementById value in a variable?

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);

What is document getElementById () value?

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.

What does the document getElementById () method return?

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.


1 Answers

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));
like image 164
slashingweapon Avatar answered Oct 18 '22 21:10

slashingweapon