March 28, 2011
How to Add text with superscript to a Button
Question by Sajja
I have a HTML button like
<input type="button" id="button" onclick="showSuper()" value="Click Me!" />
Inside JavaScript, I have
var showSuper = function() {
var btn = document.getElementById('button');
var spanSuper = "<sup>3</sup>";
btn.value="You Clicked Me " + spanSuper;
//btn.value = "You Clicked Me " + <sup>a</sup>;
}
With the above function the button value is getting replaced with You Clicked Me <sup>3</sup>
I also tried with the comment statement that also didn’t succeed. How can add a superscripted text to the button?
Answer by Starx
You do not need script to do this. Use this instead
<button name="button" id="button" onclick="showSuper()">Click Me!</button>
Script
var showSuper = function() {
var btn = document.getElementById('button');
var spanSuper = "<sup>3</sup>";
btn.innerHTML= "You Clicked Me " + spanSuper;
}