user6617662 user6617662 - 4 months ago 15
Ajax Question

I want to add echo a JS variable in my button's value field

i want to

echo
the value of capnum in my
button's
value
field
...how do i go with it?
MY code goes here -

<script type="text/javascript">
var capnum = 0;
function add()
{
capnum++;
document.getElementById('display').innerHTML = capnum;
}
</script>

<button name="subject" type="submit" value="<script> document.write(capnum) </script>" onclick="showUser(this.value);add()" >NEXT</button>

Answer

Just add id="display" to your button. I also changed .innerHTML to .value since your post says you want to change its value.

<script type="text/javascript">
    var capnum = 0;

    function add()
    {
     capnum++;
     document.getElementById('display').value = capnum;
    }

    function showUser(i) { // i just added this to test if value was changing
        alert(i);
    }
</script>

<button id="display" name="subject" type="submit" value="" onclick="add(); showUser(this.value);" >NEXT</button>

I also moved showUser after add() when I tested to make sure it worked