Berlian Berlian - 11 months ago 48
HTML Question

Input value in form input if button is clicked (in Javascript)

Here I have form in HTML

<input type="text" name ="tes[]" id="tes1">
<input type ="submit" value="Periksa Hasil" name="test">

Button in the form will use to pass the data in php. That's why I make another button to set the value of form input like this:

<button type="button" onclick="tes_ya(tes1);">Yes</button>
<button type="button" onclick="tes_no(tes1);">No</button>

So, if user click Yes, the value of form input is 1, and if user click No, the value will be 0.

I already make a javascript like this:

function tes_ya(id){
document.getElementById(id).value= 1;
function tes_no(id){
document.getElementById(id).value= 0;

But I don't know, when I try it, the value can not e set. Is there anyone can help me please? Thank you.

Answer Source

Your click handlers for the buttons have an issue:


tes1 is not defined, so when document.getElementById(id) is called with it, it's essentially calling document.getElementById(undefined) (either that or the original code will get an error and utterly fail).

What you actually want is to pass the string 'tes1':