11

What I want to do is be able to change the text inside of a button by making the text inside an actual variable.

Something like this:

<button type=button>status</button>

but instead of a string, it's a variable:

var status = 'on';
Casa_De_Agua
  • 149
  • 1
  • 1
  • 5

3 Answers3

13

If you're trying to assign the text of a button to a variable then it's as simple as following:

var buttonText = "click me!";      
document.getElementById("id-of-your-button").innerHTML = buttonText;
Reza
  • 3,473
  • 4
  • 35
  • 54
trebuchet
  • 1,483
  • 9
  • 14
5

if you are using jquery, then its a simple task.

suppose you have a button

then you can use jquery to assing its text by using

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript">
var status="on";
$(document).ready(function(){
   $("#myButton").text(val);
});
</script>

or you could pure javascript:

var status="on";
document.getElementById('myButton').innerHTML=status;
Sudeep
  • 109
  • 7
3

Guess this helps:

    <button id="myButton" type="button" value=""></button>
    <input id="myInputButton" type="button" value=""></button>

    <script type="text/javascript">
        var status = 'on';
        document.getElementById('myButton').innerHTML = status;
        document.getElementById('myInputButton').value = status;
    </script>
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57