Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  20] [ 5]  / answers: 1 / hits: 62391  / 13 Years ago, sat, december 17, 2011, 12:00:00

I'm sure I'm going to feel stupid after seeing the answer, but I keep running into prehistoric code around the web, which I'm not sure still applies. How do I change the value of a button (inside a form) using JavaScript?


Here's what I have right now:


function myFunc(form) {
form.elements["submit-button"].value = "New<br>Text";
"other stuff that actually works."
return false;
}

followed by


<form onSubmit="return myFunc(this);">
<button name="submit-button">Original<br>Text</button>
</form>

The "other stuff that actually works." actually works, so I'm sure the function is getting called and the button is getting found. I just can't seem to stick "New
Text" into the button!


Help much appreciated.


More From » html

 Answers
4

Use innerHTML instead of value.



form.elements[submit-button].innerHTML = ...


Because you are using a <button> instead of <input type=button>, you need to set the innerHTML. <button>s do not base their text on the value attribute.



<button> innerHTML </button>
<input type=button value=value />

[#88513] Thursday, December 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;