Harsh
Harsh

Reputation: 44

Trying to add text to the value of a button

I want to add a text that I send from the previous page to the value of a button, so the button has the sent value and the prior value added together. I store the value in session Storage just for context.

Example before page load:

<input type="button" id="reset" value="You have completed ">

After page load - added text is "the laundry":

<input type="button" id="reset" value="You have completed the laundry">

Basically what I tried didn't put the text within the button:

<input type="button" id="reset" value="Complete"><label><p id="result"></p></label>
<script>
    document.getElementById("result").innerHTML = sessionStorage.getItem("currentAddress");
</script>

Upvotes: 0

Views: 86

Answers (2)

F.Igor
F.Igor

Reputation: 4350

For an input, you must use the .value property to set the button text:

document.getElementById("result").value = sessionStorage.getItem("chore");

And to append to an existent text:

document.getElementById("result").value += sessionStorage.getItem("chore");

Also, You can set up the text like in this example:

document.getElementById("result").value = "You have completed " + sessionStorage.getItem("chore");

Note: If you use an HTML button <button >Content</button> instead, you can use innerHTML to set the button text.

Upvotes: 1

Hank X
Hank X

Reputation: 2044

  • you should use value, not innerHTML.
  • and use += to append the value to old value, not replace it
  • if you want to replace, just use =
  • plus you should use localStorage

    document.getElementById("result").value += localStorage.getItem("chore");

Upvotes: 0

Related Questions